Program to trim a given character from a string.
Answer Posted / sandeep ambekar
//
// MAIN -- Program to Purge a given character from a string.
// I/P example: ./a.out Bangalore a
// -- Sandeep Ambekar
#include <stdio.h>
#define TRUE 1
#define FALSE 0
void
purge_char_from_string (char *str, char *c)
{
int i, k, flag, count; // i/j for index, flag to
keep track of
// 'c' and count for iteration..
char *buf = str; // save the original string
start:
str = buf; // we could enter the loop
again.
i = 0;
k = 1;
count = 0;
flag = FALSE; // INIT variables...
printf ("Input String [%s] [%c] \n", str, *c);
while (str[i] != '\0')
{
printf ("\t (%c) <==> (%c)\n", str[i], str[k]);
if (str[i] == c[0])
{
str[i] = str[k];
flag = TRUE;
count++;
}
else if (flag == TRUE)
{
str[i] = str[k]; // later have a while loop
to find a char
// to which is !c and replace them.
}
i++;
k++;
}
printf ("[%s]\n", buf);
if ((count - 1) >= 1)
goto start;
} // end of
purge_char_from_string ....
//
// MAIN -- Program to Purge a given character from a string.
//
int
main (int argc, char *argv[])
{
if (argc < 3)
{
printf ("Input <String> <char> \n");
return 1;
}
printf (" ## Input string %s : Char [%s]\n", argv[1],
argv[2]);
purge_char_from_string (argv[1], argv[2]);
printf (" Trimmed String ---> %s\n", argv[1]);
return 0;
} // end of main..
| Is This Answer Correct ? | 2 Yes | 2 No |
Post New Answer View All Answers
how logic is used
How to implement a packet in C
What is the difference between āgā and āgā in C?
What is the use of gets and puts?
What does typedef struct mean?
What is the purpose of void in c?
illustrate the use of address operator and dereferencing operator with the help of a program guys plzzz help for this question
What is the size of a union variable?
what is bit rate & baud rate? plz give wave forms
A collection of functions,calls,subroutines or other data a) library b) header files c) set of files d) textfiles
What should malloc() do? Return a null pointer or a pointer to 0 bytes?
Can you add pointers together? Why would you?
Explain the difference between ++u and u++?
What are types of preprocessor in c?
Is c easy to learn?