Write a program in C to split string by space into words.
Sample Input/Output Dialog:
Input string: I am a 1st year student of BCC.
Output:
I
am
a
1st
year
student
of
CGA.
#include <stdio.h>
#include <string.h>
int main ()
{
char inputString[100];
char * pch;
//get string from the user
printf ("Input string: ");
fgets(inputString, sizeof inputString, stdin);
//splt string using strtok function
pch = strtok(inputString," ");
while (pch != NULL)
{
//display word in string
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
getchar();
getchar();
return 0;
}
Comments
Leave a comment