Reverse A String Using Pointers | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 26 сен 2024

Комментарии • 17

  • @justcurious1940
    @justcurious1940 7 месяцев назад +1

    Great video,Thanks :
    // we can add an other check if the string only has only 1 character and also we can ask for help from strlen() to make
    // the end pointer points to the last character, I'm not sure if it's a legal way to play with pointers
    void reverse_string(char *string){
    if (!string) return;
    // if the string only has the null terminator or only 1 character
    if (!(*string) || !(*(string+1))) return;
    char *start = string;
    char *end = start + (strlen(start)-1);
    char temp;
    while(start < end){
    temp = *start;
    *start = *end;
    *end = temp;
    ++start;
    --end;
    }
    }

  • @virtueose
    @virtueose 7 месяцев назад +2

    did u release it again or is it a deja vu

    • @PortfolioCourses
      @PortfolioCourses  7 месяцев назад

      I released it again, apologies if people got a double notice. :-S I modified the algorithm to use start < end in the loop condition, after looking into pointer comparisons a little more. I was worried a comparison with < could potentially cause a type conversion to occur (it's a long story, but technically the C standard allows for this, e.g. if pointers were represented in an unusual way), but practically this is very unlikely and the compiler will produce code that compares integers (which is going to be efficient). e.g. in GNU C it's going to be a comparison of unsigned integers: www.gnu.org/software/c-intro-and-ref/manual/html_node/Pointer-Comparison.html

  • @MrMalchore
    @MrMalchore 7 месяцев назад

    I haven't tried to reproduce this code yet, but won't the compiler complain of using arithmetic on pointer types? I know it's valid ANSI C, but I ran into annoying compiler warnings when I attempted my own method of counting the vowels in a string. (NOTE: didn't get the warnings using gcc, but DID get the warnings when I switched to VSCode and used it's compiler.)

    • @PortfolioCourses
      @PortfolioCourses  7 месяцев назад

      We shouldn't get warnings to my knowledge, the compiler arithmetic here should be OK.

  • @AnalogDude_
    @AnalogDude_ 7 месяцев назад

    With a special for loop that can work on 2 variables at the same time, you could do this with 7 lines of code, regardless even or uneven array's.
    size_t m = k;
    for(size_t l = 0; m > 0; m--, l++)

    • @justcurious1940
      @justcurious1940 7 месяцев назад

      i agree, but it won't be convenient with this example? 😗.

    • @AnalogDude_
      @AnalogDude_ 7 месяцев назад

      it might be possible to use even less lines of code combining mine and his solution. while typing his code in C++ compiler Qt, i decided to measure the length of string and go from there and create a new char array using malloc, than looping the original "string" and copy it to the new array in reverse, counting up and down at the same time and than copy it back again to the original "string".@@justcurious1940

    • @AnalogDude_
      @AnalogDude_ 7 месяцев назад

      there is a really clever for loop method possible to measure the ·string" array and it's only 1 line code and you store the loop variable outside. you can find it on stack.@@justcurious1940

  • @thehady1
    @thehady1 7 месяцев назад

    Great vid but fk C i'm currently learning it i'm done with pointer but i've understood nothing in the video otherwise 🎉

    • @PortfolioCourses
      @PortfolioCourses  7 месяцев назад +1

      This video covers an introduction to pointers, maybe it will be helpful? ruclips.net/video/2GDiXG5RfNE/видео.html

    • @thehady1
      @thehady1 7 месяцев назад +1

      @@PortfolioCourses thanks .i've got a question when you write 'char * string' it points to the array values and the pointer '*end=string' can you explain for me that part, i cannot even put my question direct? thanks in advance keeping it simple please

    • @big-jo89
      @big-jo89 7 месяцев назад +1

      ​@@thehady1
      Let's say we have this string ==> "Hello" that needs to be reversed
      first we need to declare a pointer to this string in memory char p[ ] = "Hello";
      we can also declare a pointer to a char
      char *p; but this will require allocating memory for the string using malloc then strcpy(p, "Hello"); and don't forget to free the allocated memory afterwards ==> free(p);
      anyway the pointer p is now pointing at the first byte of the string "Hello" in memory which is the character "H"
      p is here 👇
      H e l l o \0
      inside reverse_string we pass the pointer named p (string in his case) then we declare two other pointers:
      char *end = p and char *start = p both of them point at the same block of memory that p is pointing at
      we need to traverse the pointer (end) to the end of the string in order to subsequently exchange the value it references with the value pointed to by the pointer (start)
      so after the looping over the string using the pointer (end), (end) should point at the NULL char "\0" but because we do end - - after the loop the pointer (end) will point at the char "o"
      p is here 👇
      H e l l o \0
      start is here 👆 👆 end is here after decrementing the pointer by 1
      then you just swap the values by de-referencing each pointer and increment start and decrementing end
      p is here 👇
      o e l l H \0
      ☝ ☝
      start end
      p is here 👇
      o l l e H \0

      start end
      p is here 👇
      o l l e H \0
      ☝ ☝
      end start
      at this point the loop should exit because start is greater than end
      now in main we print the value that (p) is pointing at in memory therefore it should print ==> olleH

    • @thehady1
      @thehady1 7 месяцев назад

      @@big-jo89 thanks

    • @justcurious1940
      @justcurious1940 7 месяцев назад

      @@big-jo89 Amazing bro, amazing usage of emojis.🙃