Sunday, December 1, 2013

C program to reverse words in sentence

This program reverse the words in given sentence.


#include<stdio.h>

int main()
{

     char word[100] = "The world is so vast. If you have will power you can get it";
     int  space_index[10];
     int i = 0;
     int j = 0;
     int k = 0;
     int len = 0;
     int end = 0;
     int start = 0;
     while(word[i] != '\0')
     {
        if(word[i] == ' ')
        {
            space_index[j++] = i;      
        }          
       
        len++;          
        i++;
       
     } // End of While   
        end = --len;
      while((--j) >= 0)
      {
       start = space_index[j];
      for( k = start;k<=end;k++)
           printf("%c",word[k]);
        end = start;
      }
        if(j<0)
          {
      for( k = 0;k<=end;k++)
           printf("%c",word[k]);
 
          }
   getch();
      }

No comments:

Post a Comment