Reverse A String Using Pointers | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

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;
}
}

justcurious
Автор

did u release it again or is it a deja vu

virtueose
Автор

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.)

MrMalchore
Автор

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++)

AnalogDude_
Автор

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

thehady