Monday, 9 November 2015

C++ Program To Print Reverse Sentence

problem :- Write A C++ Program To Print Reverse Sentence .

Logic :- Like Reverse String This is a reverse String I Prefer A Program First See than Try to Understand C Program For Reverse A String Without (srtrev) Using Library Function .Now try By Yourself If any thing You are not getting Comment Below .

Solution :-

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
  int i,prev,next,j,k;
  //prev=0;
  char st[100];
 
  cout<<"Enter The Sentence : \n";
  gets(st);
 
  for(i=0;st[i]!='\0';i++) 
  prev=i;
 
  cout<<"\nEntered Sentence 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:-

C++ Program To Print Reverse Sentence

No comments:
Write comments

Recommended Posts × +