Skip to main content

Featured

functions in Python

C++ Program to print reverse string word by word

//C++ Program to print reverse string word by word
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j,x,sp=0;
cout<<"Enter a String \n \n";
char str[40];
gets(str);
strcat(str," ");
x=strlen(str);
for(int y=0;str[y]!='\0';y++)
{
if(str[y]==' ')
sp++;
}
for(int k=1;k<=sp;k++)
{ if(k<sp)
{
for(i=x-2;str[i]!=' ';i--);
}
  else if(k==sp)
{
i=-1;
}

 for(j=i+1;str[j]!=' ';j++)
 {
 cout<<str[j];
 }
 cout<<" ";
 x=i;
}
getch();
}

OUTPUT:

Comments

Popular Posts