Skip to content

Instantly share code, notes, and snippets.

@LinyinWu
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LinyinWu/a8fc47eef2993e3bc749 to your computer and use it in GitHub Desktop.
Save LinyinWu/a8fc47eef2993e3bc749 to your computer and use it in GitHub Desktop.
/*【解题思路】
c语言,char指针来对字符串进行操作,先找到尾指针,然后前后in place交换。
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/LinyinWu/a8fc47eef2993e3bc749
【test】
hello
(空)
hello world
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverseString(char * str) {
if(str == NULL)
return;
char* end = str;
while(*end != '\0') {
end++;
}
end--;
while(str<end) {
char temp = *str;
*str = *end;
*end = temp;
str++;
end--;
}
}
int main() {
char in[256];
printf("enter the string:\n");
fgets (in, sizeof(in), stdin);
if ((strlen(in)>0) && (in[strlen (in) - 1] == '\n'))
in[strlen (in) - 1] = '\0';
char * str = in;
printf("before reverse: %s\n", in);
reverseString(str);
printf("after reverse: %s\n", in);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment