Thursday, September 11, 2008

c++ program to reverse a sentence

#include < iostream.h >
#include < stdio.h >
#define MAX 100
#include < string.h>



void swap(char* , char*);

void str_rev(char *str)
{

char *fir = str;
char *end = str + (strlen(str) - 1);
swap(fir,end);

char *x = str;
char *y = x;

while (*x != '\0')
{
if(*x == ' ')
{
swap(y,x-1);
x=x+1;
y=x;
}

else
{
x++;
}

}
swap(y,x-1);

}

void swap(char *fir, char *end)
{
char temp;

while(fir < end)
{
temp = *fir;
*fir = *end;
*end = temp;
fir++;
end--;
}
}




void main()
{
printf( " enter text \n");
char text[MAX];
gets(text);
str_rev(text);
printf( " %s", text);
}

0 comments: