C++ Program – Reversing each word in a string

by Nideesh C on May 2, 2011 · 0 comments

in C++




//Downloaded From theonlinetutorials.com
//(C)2011.All rights reserved.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
clrscr();
int  ch,i,j,k=0,p=0,l;
char st[50],sr[50];
cout<<"Enter Your Choice : "<<endl<<"1.Each Word Reversing"<<endl<<"2.Whole Sentence Reversing"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the String"<<endl;
gets(st);
l=strlen(st);
 for(i=0;i<=l;i++)
 {
 if((st[i]==' ')||(st[i]=='\0'))
 {
 for(j=i-1;j>=p;j--)
 {
 sr[k]=st[j];
 k++;
 }
 sr[k]=' ';
 k++;
 p=i+1;
 }
 }
 for(i=0;i<p;i++)
 cout<<sr[i];
break;

case 2:
cout<<"Enter the String"<<endl;
gets(st);
l=strlen(st);
p=l-1;
 for(i=l-1;i>=-1;i--)
 {
 if((st[i]==' ')||(i == -1))
 {
 for(j=i+1;j<=p;j++)
 {
 sr[k]=st[j];
 k++;
 }
 sr[k]=' ';
 k++;
 p=i-1;
 }
 }
for(i=0;i<=l;i++)
cout<<sr[i];
break;

default:
cout<<"Wrong Choice"<<endl;
break;

}

getch();
}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ Program – Calculator
  2. C++ Program – Convert decimal to binary & binary to decimal
  3. Sample – Program to enter a string and find its length.
  4. C++ Program – Convert Binary to Octal & Octal to Binary
  5. C program – Morse code to text conversion and vice-versa.

Leave a Comment

Previous post:

Next post: