My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to reverse each word of the string

Tuesday, September 1, 2009

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{  clrscr();
   char s[500];
   cout<<"enter the string:"<<endl;
   gets(s);
   int i,j,m,n,k,g,p,l;
   i=j=m=n=k=p=g=0;

   while(s[i]!='\0')
   {  p++;i++;     }
   i=0;

   while(i<=p)
   {
     if(s[i]==' '||s[i]=='\t'||s[i]=='\0')
     {   l=0;
         m=i-k;
         k=i+1;


         for(j=0;j<m/2;j++)
         {  n=s[i-l-1];
        s[i-l-1]=s[i-m+l];
        s[i-m+l]=n;
        l++;

           }

       }
       i++;
   /*       if(s[p-1]!=' '||s[p-1]!='\t')
        {   j=0;
         while(s[p-j-1]!=' '||s[p-j-1]!='\t')
         {
          g=g+1;
          j=j+1;      }
          l=0;

          for(j=0;j<g/2;j++)
          {    n=s[p-l-1];
               s[p-l-1]=s[p-g+l];
               s[p-g+l]=n;
               l++;               }


         }           */
    }
    cout<<"string after reverse the words is:"<<endl;
    cout<<s;
    getch();
    }

0 comments: