Program to reverse the letters of each words of the string
#include<stdio.h>
#include<conio.h>
void reverse(char *start, char *end);
void reversethewords(char *s)
{
char *wordbegin = s; //wordbegin and temp are two pointer to the first character of string s
char *t = s;
while( *t)
{
t++;
if (*t == '\0') // if reached at the end of the string reverse(swap) the first character and last and
{ so on.
reverse(wordbegin, t-1);
}
else if(*t == ' ') // if encountered with space then reverse and make wordbegin=t+1 for the
{ next word
reverse(wordbegin, t-1);
wordbegin = t+1;
}
}
}
void reverse(char *start, char *end)
{
char temp;
while (start < end)
{
temp = *start;
*start++ = *end;
*end-- = temp;
}
}
int main()
{
char s[] = "I love this blog";
reversethewords(s);
printf("%s", s);
getch();
}
Output-"I evol siht golb"
Comments
Post a Comment