Monday 30 July 2012

Write a program to reverse ( in place ) a string.



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void reverse(char *begin, char *end)
{
    char temp;
    while(begin<end)
    {
        temp=*begin;
        *begin++ = *end;
        *end-- = temp;
    }
  /*  src=src+strlen(src)-1;
     while(*des++ = *src--);  */
}


reverseword(char *s)
{
    char *start,*temp;
    start=temp=s;
    while(*temp)
    {
        temp++;
        if(*temp==' '|| *temp=='\0')
        {
            reverse(start,temp-1);
            start=temp+1;
        }
    }
    reverse(s,temp-1);
}
int main()
{
char s[]="This is a good boy";
reverseword(s);
printf("%s\n",s);
return 0;
}


Output: boy good a is This

No comments:

Post a Comment