Reverse A String | C Programming Example

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

can you pls share the time and space complexity of this code?

VAISHNAVIV-lgzb
Автор

congrats sir for 1.1k subs...i have a question...can this function be created with char* as return type instead of void?

mongraal
Автор

bro thx so much love how it turned out :)

Hamah
Автор

here's the code
#include <stdio.h>
#include <string.h>

void reverse(char *string) {
int length = strlen(string);
int middle = length / 2;
char temp;

for (int i = 0; i < middle; i++) {
temp = string[i];
string[i] = string[length - i - 1];
string[length - i - 1] = temp;
}
}

int main() {
char test[] = "one small step for man";
printf("Original string: %s\n", test);
reverse(test);
printf("Reversed string: %s\n", test);

return 0;
}

tox_anituber
Автор

How would this work with a string input and new lines in the string ex:
Hello there
Hey

jackvespaziani
Автор

how about reversing one sentence. form this "I drink tea" into "tea drink I".

fathanrafaa
Автор

How do you maintain the likes of position such as keeping (a) as is and not swapping those characters?

michaelk
Автор

is there a benefit doing this rather than using strrev() function that is included in string.h?

lenardskie
Автор

can you share more light on string[length - i -1] ?

josephjohn
join shbcf.ru