954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need to output backwards not in reverse

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;
}
carlcarman
Newbie Poster
22 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 
memove() does not return anything

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
gets( string );

Q: Why does everyone say not to use gets()?

And could you clarify your expected input and output with an example?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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.

carlcarman
Newbie Poster
22 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.

carlcarman
Newbie Poster
22 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Are you allowed to use recursion?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

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
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

While driving into work I realized I had a bug.

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

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You