本文共 972 字,大约阅读时间需要 3 分钟。
有一个字符数组的内容为:"i am a student",
请你将数组的内容改为"student a am i".要求:不能使用库函数。只能开辟有限个空间(空间个数和字符串的长度无关)。#include#include int MyStrlen(char *str){ int count = 0; while (*str != '\0') { str++; count++; } return count;}void Reverse(char *left, char *right){ char temp; while (left < right) { temp = *left; *left = *right; *right = temp; left++; right--; }}void ReverseSentence(char *str){ char *left = str; char *right = str + MyStrlen(str) - 1; char *p=str; Reverse(left,right); while (*p != '\0') { char *b = p; while (*p != ' '&&*p != '\0') { p++; } Reverse(b, p-1); if (*p==' ') { p++; } }}int main(){ char str[] = "i am a student"; ReverseSentence(str); printf("%s\n",str); system("pause"); return 0;}
c语言的灵魂》》》指针
转载于:https://blog.51cto.com/14233078/2380945