Friday, February 11, 2005

Reversing words in a sentence

#include <stdio.h>

int reverse(char* start, char* end) {
char tmp;
while (start < end) {
tmp=*start;
*start=*end;
*end=tmp;
start++;
end--;
}
return 0;
}

int main (int argc, char** argv) {

char test[]="A loves B";
char* start=&test[0];
char* end=start+strlen(test)-1;
char* begin;
char* stop;
reverse(start,end);
printf ("%s\n",test);
begin=start;
stop=start;
while (stop<end) {
while (*stop!=' ' && stop!=end)
stop++;
reverse(begin,stop-1);
begin=stop+1;
stop=begin;
}
printf ("%s\n",test);
return 0;

}

0 Comments:

Post a Comment

<< Home