Monday 30 January 2017

Reversing every word in a given line of text: C program

Reversing every word in a given line of text
Program:
#include <stdio.h>
#include <string.h>
main()
{
char a[100],*token,st[10][50];
int i=0,j,l;
puts("enter line of text");
gets(a);
token = strtok(a," ");
while(token != NULL)
{
 strcpy(st[i],token);
 token=strtok(NULL," ");
 i++;
}
l=--i;
for(j=0;j<i;j++,i--)
{
     strcpy(a,st[j]);
     strcpy(st[j],st[i]);
     strcpy(st[i],a);
}
for(j=0;j<=l;j++)
 printf("\n%s",st[j]);

}

No comments:

Post a Comment