memove() does not return anything
[aside]Take a closer look at that linked reference.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Dave Sinkula
long time no c
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
Are you allowed to use recursion?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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