Problem:- Reverse words in a given string or how to perform reversing a sentence Word by Word in C or C Program to Reverse every Word of given String or Reverse of Words in Given String using C or Reverse the Order of Words in a String or C Program to reverse each words in a string without reversing the characters or C Program to Reverse Letter in Each Word of the Entered String or C Program to reverse the sentence by words or How to reverse words in a sentence using C.
Explanation:- reversing the sentence word by word is not similar to reverse the sentence or string both problems are different. in reversing the sentences we run a Loop from the last index to first index and print the string but here we have to print the sentence word by word without changing the meaning of the words and So for this problem, first reverse the full sentence by using the strrev() library function now string array last index in become first index now run a loop until space and Null condition occurs, NULL condition for the last index of a string array So if the condition is true then print the character from last space to string next space and repeat the process again and again. After each word print space and if the condition is not true then continue the Loop here continue statement send the control back to Loop.
Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page.
Solution:-
#include<stdio.h>
#include<string.h>
void main()
{
/*Visit - www.programmingwithbasics.com*/
printf("=====================================");
printf("\nVisit - www.programmingwithbasics.com");
printf("\n=====================================");
char str[100];
int i,j;
puts("\n\nEnter The String Want To Reverse Word By Word:\n");
gets(str);
printf("\nString After Word By Word Reverse \n\n");
strrev(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i+1]==' ' || str[i+1]==NULL)
{
for(j=i; j>=0 && str[j]!=' '; j--)
printf("%c",str[j]);
}
else
continue;
printf(" ");
}
getch();
}
Output:-
You May Also See
9. C++ Program To Find The GCD And LCM Of Two Numbers
10. C++ Program To Find Whether A Number Is Palindrome Or Not
thank u sir it is very helpful , for me. once again thank u very much.
ReplyDeleteFinally got a solution
ReplyDelete