Added notebook solving the following: Implement the function void Reverse(char* str).

This commit is contained in:
Donne Martin
2015-05-01 08:13:47 -04:00
parent 95591ef96e
commit 51cab5e686
3 changed files with 191 additions and 0 deletions

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