C Code for reversing sentence characterwise

C program to reverse a sentence character wise
#include<stdio.h>
void reverse1(char *x);
void reverse2(char *first, char *last);
int main()
{
char sentence[40];
printf("Enter few string:\n");
gets(sentence);
printf("You Entered:%s\n",sentence);
reverse1(sentence);

return 0;
}
void reverse1(char *s)
{
char *temp;
char *word;

temp=s;
word=s;

while(*temp)
{
temp++;
}
reverse2(s,temp-1);//then reverse the sentence
printf("Reversed String is:%s\n",s);
}

void reverse2(char *firstt, char *lastt)
{
char t;
while(firstt<lastt)
{
t=*firstt;
*firstt=*lastt;
*lastt=t;
firstt++;
lastt--;
}
}
/*
Enter few string:
India is Great
You Entered:India is Great
Reversed String is:taerG si aidnI
*/

No comments:

Post a Comment