Q:

C++ Program To Reverse a Sentence or String

0

Logic:-

 Like reverse string, this is a reverse sentence. So first we are taking an input from the user after that we are finding the string or sentence last index point so we can reverse a sentence while reversing a sentence if any space occurs we take and space and print space after that again string starts printing. I  strongly prefer a program first try to understand this program so can understand the logic basically what is going on.

Now try by yourself if you are getting problem Comment Below and also sure that you have read the full post.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{

   
    cout<<"=====================================";
    cout<<"\nVisit - www.nerdutella.com";
    cout<<"\n=====================================";
    
   int i, prev, next, j, k;
  //prev=0;
   char st[100];

   cout<<"\n\nEnter The Sentence: ";
   gets(st);

   for(i=0;st[i]!='\0';i++) 
   prev=i;

   cout<<"\n\nSentence In Reverse Order Is As Follow \n\n";
   for(k=i-1;k>=0;k--)
   { 
    if(st[k]==' '||k==0)
    {
     if(k==0)
     {
     next=0;
 }
     else
     {
     next=k+1;
 }
   
     for(j=next;j<=prev;j++)
     {
     cout<<st[j];
     } 
 cout<<" ";
     prev=next-2;  
    }
   }

   return 0;
}

 

Output:

=====================================

Visit - www.nerdutella.com

=====================================

Enter The Sentence: c++ programming

Sentence In Reverse Order Is As Follow 

programming c++ 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program To Print A String... >>
<< C++ Program To Convert String Lowercase To Upperca...