Monday, 4 April 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



Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

1 comment:

  1. thank u sir it is very helpful , for me. once again thank u very much.



    ReplyDelete