this code has some bugs Im new to c strings. the code that i have below reverses each word. For example. i entered "house is" it reverses it to "si esuoh" I want it to reverse it by word like "is house".

My other questions since strings are pointers how do I call each one in an array?

here is my code

Code:

#include <stdio.h>
#include <string.h>

void reverse(char *sPtr);

int main (void)

{

char sentence[80];

printf( "enter a line of text:\n");

fgets(sentence,80, stdin );

printf("\nThe line printed backwards is\n");
reverse( sentence );

return 0;

}

void reverse ( char *sPtr){

if(sPtr[0] == '\0') {
return;}

else {
reverse(&sPtr[1]);
putchar(sPtr[0]);
}

}

02-08-2011

GReaper

You need to break the sentence to each phrase, and then print them reversed.

02-08-2011

newbc

reverse it like tokens?

02-08-2011

GReaper

Quote:

Originally Posted by newbc

reverse it like tokens?

Yes, but this time you'll read one word with each iteration.

02-08-2011

Babkockdood

I would suggest calling your function every time there's a space, and breaking up strings into individual words.

02-08-2011

ssharish2005

The solution would be pretty straight forward. Use strtok (although i'm not very about on strtok as it alters the orginal string. but for beginning should be ok) to tokensie the string with the ' ' as a deliminator. And call the the your reverse function on every token.