NO

Author Topic: "Reverse string" does not work.  (Read 3783 times)

Langer.197

  • Guest
"Reverse string" does not work.
« on: January 03, 2013, 07:07:57 PM »
Hey guys!

I am a total beginner in programming, just started two months ago, and i need your help.

Here is my task very quick, the problem will follow afterwards.

TASK:
I got a file, in which i wrote stuff over three lines. Now i have to create a second file, copying the same text, but reversing line by line.
So for example:

"you
look
good."

should look like this:
"uoy
kood
.doog"

I made it work to copy the content, but can not figure out the code to reverse the strings.

So here is my code, would be great if i could get some help. The function call is required.

Thanks alot in advance.

Code: [Select]
reverse(line);
This is my function call.

Code: [Select]
void reverse (char line []){

int i;
int length;
char tmp;

length = strlen(line);

for( i=0 ; i <=length/2 ; i++){
        tmp = line[i];
line[i] = line[length - i];
line[length - i]= tmp;
}

return;
}

And thats my function.

CommonTater

  • Guest
Re: "Reverse string" does not work.
« Reply #1 on: January 03, 2013, 07:51:56 PM »
Ok couple of things....
 
A string is actually an array.  In C all array indexes begin at 0 and go to size-1 ... thus a 10 element array will have elements numbered 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9... go ahead count them, that's 10. 
 
Now, take a look at your loops and swaps...
 
When the string is length 10 ... where does your loop exit?
When the index i is at 0 which element is the swap accessing with length - i ?
 
These are called bounds errors and they can lead you to crashing your program... or at least getting some truly magical results.
 
The fix is very simple... but in the interests of your education, we'll let you figure it out by yourself.
« Last Edit: January 03, 2013, 08:05:08 PM by CommonTater »

Langer.197

  • Guest
Re: "Reverse string" does not work.
« Reply #2 on: January 03, 2013, 08:09:48 PM »
Oh no. Such a stupid mistake.  :-[
Forgot the "-1" in my loop.

When the index is at 0 i need do swap it with the 10th element, wich is at my 9th index => 10 -1 -i.


Thank you very much! :)

CommonTater

  • Guest
Re: "Reverse string" does not work.
« Reply #3 on: January 03, 2013, 09:30:47 PM »
#9 is actually your 10th element in the array.
 
But, there's an easier way than having to change every reference like that...

length = strlen(string) - 1;

Then you don't need to mess with all the -1s all over the place, like this...

Code: [Select]
void reverse (char* line){

   int i;
   int length;
   char tmp;

   length = strlen(line) - 1;
   
   for( i=0 ; i < length/2 ; i++){
           tmp = line[i];
      line[i] = line[length - i];
      line[length - i]= tmp;
   }
    return;
}

Hint: Always look for the simplest solutions.  "An" answer is no where near as good as "the" answer :D



Langer.197

  • Guest
Re: "Reverse string" does not work.
« Reply #4 on: January 04, 2013, 12:18:29 PM »
Thanks! :)