Question: C PROGRAM!!! Here is the code of tokenizes a string.Please help me revise it to fulltill the new requirments: 1.Each token can be separated by
C PROGRAM!!!
Here is the code of tokenizes a string.Please help me revise it to fulltill the new requirments:
1.Each token can be separated by any number of spaces in between. Regarding trailing whitespaces, they should be ignored while looking for tokens.
For example: I(space)(space)(space)am or I(tab)(tab)am will be treated as I(space)am or I(tab)am.
2.Ignore the leading and trailing spaces.
For example: (tab)I(space)am.(space)(space) will be treated as I am.
3.remove extra space line after the last last token before print the entire string.
Input:(tab)Have(space)(space)a nice day, my(tab)(tab)dear.(tab)
Output before revise:
(blank line)
Have
(blank line)
a
nice
day,
my
(blank line)
dear.
(blank line)
(blank line)
(tab)Have(space)(space)a nice day, my(tab)(tab)dear.(tab)
excepted output after revise:
Have
a
nice
day,
my
dear.
(tab)Have(space)(space)a nice day, my(tab)(tab)dear.(tab)
CODE:
#include
#include
int main()
{
char string1[80];
char String2[10][10];
int i,j,counter;
printf(" Input a string entered by the user : ");
//reads the string
fgets(string1, sizeof string1, stdin);
j=0; counter=0;
for(i=0;i<=(strlen(string1));i++)
{
// if space or NULL found, assign NULL into String2[counter]
if(string1[i]=='\040'||string1[i]=='\0'||string1[i]=='\t')
{
String2[counter][j]='\0';
counter++; //for next word
j=0; //for next word, init index to 0
}
else
{
String2[counter][j]=string1[i];
j++;
}
}
printf(" Strings after split by space or a tab are : ");
//printing each token saperately in a new line
for(i=0;i < counter;i++)
printf(" %s ",String2[i]);
//printing the entire string in a line
printf("%s",string1);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
