my program needs to have the user enter a string and have it output backwards, not in reverse order. This is what I have so far, any help would be appreciated. I was trying to use memmove but I think I'm going in the wrong direction. Thanks Carl.

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

int main(void)
{
	char string [ 80 ];
	char *tokenPtr;
	
	printf( "enter text\n" );
	
	gets( string );
	
	tokenPtr = strtok( string, "" "");
	
	while ( tokenPtr != NULL ) {
		printf( "%s\n", tokenPtr );
		tokenPtr = strtok( NULL, "" "" );
		}
		printf( "%s%s\n", "the text before memmove is:", string );
		printf( "%s%s\n", "the text after memmove is:",
		        memmove( string, &string[5], 10 )); 
	system("PAUSE");
	
	return 0;
}

Recommended Answers

All 12 Replies

strtok() has bug's, so you have to be carefull with it. Why would you even need strtok() for this?
memove() does not return anything, so your printf statment look's... odd.
What do you mean by "output backwards, not in reverse order"? Give us a runtime example.

memove() does not return anything

[aside]Take a closer look at that linked reference.

Write a string reverse function to reverse a string from one pointer of N bytes.

First reverse the entire string.
Then parse the string looking for 1st last character of each word, then reverse those letters. Step to next word, reverse. Repeat.
Now string is in reverse word order!

using strtok is part of the assignment. it is supposed to read the text backwards. Ex. I had a dog, entered . the output should be
dog a had I.

the expected input would be text, EX. "i had a bike". the output should be "bike a had i". thanks for any advise you can offer.

Thanks Dave. :P
The simplest way to do this would probably be recursion.
EDIT: Didn't see your post Salem. I need to refresh my browser more often. :P

So use strtok() as the parser, but doesn't the method I pointed out still work?

n = strlen(str)
strrev( str, n )
q = str
p = strtok( str, " " )   # Get 'new' 1st word

while( p )
   n = (int)( p - q )     #Get length of sub-string
   strrev( q, n )          # reverse that sub-string
   q = p                     # Advance pointer for next time
   p = strtok( NULL, " " )     # Find Next 'previous' word
end while

Hmm! a problem!

"i had a bike"
"ekib a dah i" <-- First the full string reverse
"bikea dah i" <-- First reversal !RATS!

Let's do some code corrections!

n = strlen(str)
strrev( str, n )
q = str
j = 0;

while( p )
   i = (int)( p - q )     #Get length of sub-string
   strrev( q+j, i )          # reverse that sub-string
   n -= i;
   q = p                     # Advance pointer for next time
   p = strtok( NULL, " " )     # Find Next 'previous' word
   j = 1;
end while
   strrev( q+j, n-j );

"bike a dah i"
"bike a had i"
"bike a had i "

Okay, I think this does it!
First only letters are swapped. Delimiters are left as is! j=0 on 1st word, after that, delimiters are skipped!

While driving into work I realized I had a bug.

// strrev( q+j, i )
strrev( q+j, i-j )

commented: Your dedicated to the right solution. +4

never mind I really goofed up. After each strtok you'll need to do a strlen and then reverse that chunk! Not the pointer math I was trying to do!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.