×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Accept a String and Reverse It - String
Accept a String and Reverse Each Word of it - String - String - C Language
2179    Arnab De    26/05/2019

Read a string from user using gets () function. Now travel the string until a space (ASCII Code 32) found and copy the characters in a variable called 'word'. If a space found, then it considers a word and terminate it with a null character. Now reverse the word and store it into an output string. This procedure remains continue until the string is ended.

softetechnologies
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
	char str[30],rev[30],word[30],i,w=0;
	printf("Enter A String: ");
	gets(str);//READ A STRING
	strcat(str," ");//ADD A SPACE AT END OF STR
	strcpy(word,"");//BLANK THE TEMPORARY WORD 
	strcpy(rev,"");//BLANK THE OUTPUT STRING [REV]
	for(i=0;str[i]!='\0';i++)
	{
		if(str[i]!=' ')
		{
			//CHARACTER ATTACH TO WORD
			word[w++]=str[i];
		}
		else
		{
			word[w]='\0';
			strrev(word);//reverse the word
			//attach the reversed word into output string [rev]
			if(strlen(rev)==0)
			{
				strcpy(rev,word);
			}
			else
			{
				strcat(rev," ");
				strcat(rev,word);
			}
			//RESET THE VARIABLE WORD AND W
			strcpy(word,"");
			w=0;
		}
	}
	//PRINT THE OUTPUT
	printf("Result : ");
	puts(rev);
	return 0;
}
softetechnologies
Accept a String and Reverse It - String
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
Fill the brain with high thoughts, highest idrals, place them day and night before you, and out of that will come great work.
Swami Vivekananda
1024
79.79
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     53897
25/06/2018     44964
01/01/2018     43545
28/06/2017     41094
02/08/2017     40074
01/08/2017     34126
06/07/2017     33951
15/05/2017     33195
11/09/2018     30035
14/07/2017     29671
softetechnologies