Explain this C code to change the line

I really don't understand what the last while loop is doing, can anyone explain this?

void reverse(char *str) { 
    char * end = str; 
    char tmp; 

    if (str) {
        while (*end) { 
            ++end;
        }
        --end; 
        while (str < end) {
            tmp = *str; 
            *str++ = *end;
            *end-- = tmp;
        }
    }
}

Can someone get me through the hello example?

+3
source share
5 answers

What basically happens in this inner while loop, so that at each iteration, the characters that are pointed strand endswapped strget incremented to indicate the next character, and endreduced to indicate the previous one.

Using hello as an example:

v   v
hello
 v v 
oellh
  v
olleh

And then the cycle ends, like str= end.

+4
source

The main idea of ​​this code is to work in two passes:

  • .
  • , , .

:

char *end = str;
while (*end) { 
    ++end;
}
--end; 

while end, . end , *end true. C , true, end - , . , , end . --end . end . "Hello:"

 H e l l o
 ^       ^
 |       |
str     end

, end, . :

while (str < end) {
    tmp = *str; 
    *str++ = *end;
    *end-- = tmp;
}

, , , , , .

while (str < end) {
    tmp = *str; 
    *str = *end;
    *end = tmp;

    ++str;
    --end;
}

, . , , . :

 H e l l o
 ^       ^
 |       |
str     end

 o e l l H
   ^   ^
   |   |
  str end

 o l l e H
     ^
     |
  str end

, .

, , , . , . , :

char *end = str;
while (*end) { 
    ++end;
}
--end; 

, end . , , end ! , undefined . , , , :

while (str < end)

, end - , .

, !

+11

, ; end .

The second, when he exchanges the character marked strwith the one indicated end, and moves strforward from 1 character and endbackward from 1 character. Thus, characters are swapped from the outer end of the string to the inner.

Appearance ends when endthe character begins to point to str, which means that we have reached the center of the line, and all the characters have already been replaced.

Hello example:

S   E            S   E
V   V            V   V
Hello  >>swap>>  oellH

>>pointer increment/decrement>>

 S E              S E
 V V              V V
oellH  >>swap>>  olleH

>>pointer increment/decrement>>
it would result in

  E                
  S                
  V                
olleH

but now str is no longer before end, so the loop terminates
0
source

Well, what is it.

void reverse(char *str) { 
    char * end = str;  // copy the pointer 
    char tmp; 

    if (str) {
        // Now step through the string until you get to the last character.
        // which will be the null terminator \0
        while (*end) { 
            ++end;
        }
        // One step back, and we're pointing at the last actual character.
        --end; 
        // Now.  Move the start at either end and work your way inwards until
        // they meet in the middle.
        while (str < end) {
            // take copy of left character into tmp
            tmp = *str; 
            // copy right character into left position and move left pointer right
            *str++ = *end;
            // copy tmp character into right position and move right pointer left
            *end-- = tmp;
        }
    }
}
0
source
#include<stdio.h>
#include<string.h>
main()
{
  int i=0,lenght,j;
  char str[100],st[100];
  printf("\nEnter a string");
  scanf("%s",str);
  lenght=strlen(str);
  for(i=0,j=(lenght-1);str[i]!='\0',j>=0;i++,j--)
   {
      st[j]=str[i];
   }
  st[i]='\0';
  printf("\nThe reversed string is %s\n",st);
 }
-1
source

Source: https://habr.com/ru/post/1793739/


All Articles