Program in C to reverse the words

Write a program in C to reverse the words

#include <stdio.h>
#include <conio.h>
#include <string.h>

const int opt = 50;

int main()
{
          int i=0,j=0,c=0,vali[opt] = {0};
          char str1[opt],str2[opt]="",str3[opt]="",ch;
          printf("please enter your sentence :");
          gets(str1);
          for(i=0;str1[i] != '\0';i++)
          {
                   if(str1[i] == ' ')
                   {
                             vali[j+1] = i;
                             j++;
                   }
          }
          c = j;
          for(i=0;i<=j && c!=0;i++)
          {
                   strcat(str2,&str1[vali[c]+1]);
                   strcpy(&str1[vali[c]],str3);
                   strcat(str2," ");
                   c--;
          }
          strcat(str2,str1);
          printf("\nyour reversed sentence is :");
          puts(str2);
          getch();
          return 0;
}


Post a Comment