翻转字符串
有一个字符数组的内容为“student a am i”,请你将数组的内容改为“i am a student”
不能使用库函数。只能开辟有限个空间(空间个数和字符串的长度无关)
#include <stdio.h> #include<stdlib.h> void reverse(char *left, char *right) { while(left<right) { char tmp = *left; *left = *right; *right = tmp; left++; right--; } } void reverse_str(char arr[], int sz) { char *left = arr; char *right = arr+sz-1; char *start = arr; char *end = arr; reverse(left, right); while(*end != '\0') { while((*end != ' ') && (*end != '\0')) { end++; } reverse(start, end-1); if(*end != '\0') { start = end+1; end = start; } } } int main() { char arr[] = "student a am i"; int sz = sizeof(arr)/sizeof(arr[0])-1; reverse_str(arr, sz); printf("%s\n",arr); system("pause"); return 0; }