Sunday, January 29, 2012

C ++ Program to Reverse a String Word By Word . . .

Hi Buddies , this is a small program that helps to reverse a given string word by word .

Example , If u give input as "I am studying in USA" means it will return output as "I ma gniyduts ni ASU"




int main()
{
    int i,j=0,len,k=0,m=0;
   char text[30];
   clrscr();
   gets(text);
    char temp[30],temp2[5];
    len=strlen(text);
    text[len+1]=' ';
    for(i=0;i<len;i++)
    {
 if(text[i]==' ')
 {
     i++;
 }
 else
 {
     while(text[i]!=' ')
     {
      temp[j]=text[i];
      i++;
      k++;
      j++;
     }
     for(m=k-1;m>=0;m--)
     {
     printf("%c",temp[m]);
     }
     cout<<" ";
     j=0;
     k=0;
 }
    }
    getch();
}


No comments:

Post a Comment