Answer to Question #185631 in C for Rui

Question #185631

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.


1
Expert's answer
2021-04-26T05:41:09-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS