04/04/2016

C Program to Reverse The Words in a Sentence

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.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in 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.


Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

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:-

C Program to Reverse The Words in a Sentence

You May Also See


1. C Program To Insert An Element Desired or Specific Position In An Array



Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

2 comments: