mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 10:58:02 +00:00
Renamed top-level folders to use underscores instead of dashes.
This commit is contained in:
36
arrays_strings/reverse_string/reverse_string.cpp
Normal file
36
arrays_strings/reverse_string/reverse_string.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void Reverse(char* str) {
|
||||
if (str) {
|
||||
char* i = str; // first letter
|
||||
char* j = str; // last letter
|
||||
|
||||
// find the end of the string
|
||||
while (*j) {
|
||||
j++;
|
||||
}
|
||||
|
||||
// don't point to the null terminator
|
||||
j--;
|
||||
|
||||
char tmp;
|
||||
|
||||
// swap chars to reverse the string
|
||||
while (i < j) {
|
||||
tmp = *i;
|
||||
*i++ = *j;
|
||||
*j-- = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
char test0[] = "";
|
||||
char test1[] = "foo";
|
||||
Reverse(NULL);
|
||||
Reverse(test0);
|
||||
Reverse(test1);
|
||||
printf("%s \n", test0);
|
||||
printf("%s \n", test1);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user