Write a Program in C++ for Reversing each word of a Sentence.
//Program to Reverse Each Word of a String.
#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char line[80];
int n, i, j=0, wlen=0;
clrscr();
cout<<"Enter any string of your choice : ";
cin.getline(line,80);
strcat(line," ");
n=strlen(line);
for(i=0;i<n;i++)
{
if(line[i]==' ')
{
for(j=i-1;j>=i-wlen;j--)
cout<<line[j];
cout<<" ";
wlen=0;
}
else
{
wlen++;
}
}
getch();
}
#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char line[80];
int n, i, j=0, wlen=0;
clrscr();
cout<<"Enter any string of your choice : ";
cin.getline(line,80);
strcat(line," ");
n=strlen(line);
for(i=0;i<n;i++)
{
if(line[i]==' ')
{
for(j=i-1;j>=i-wlen;j--)
cout<<line[j];
cout<<" ";
wlen=0;
}
else
{
wlen++;
}
}
getch();
}
No comments
Post your comments