#include<stdio.h> #include<assert.h> /*求字符串长度*/ int my_strlen(char *str) { assert(str); int count=0; while(*str) { count++; str++; } return count; } /*逆置函数*/ char *reverse_str(char *start,char *end) { char *ret=start; char temp; while(start<end) { temp=*start; *start=*end; *end=temp; start++; end--; } return ret; } char *reverse(char *str) { assert(str); char *tmp=str; char *start=str; char *end=str+my_strlen(str)-1; reverse_str(start,end);//逆置字符串 while(*str) { start=str; while((*str!=‘ ‘)&&(*str!=‘\0‘)) { str++; } end=str-1; reverse_str(start,end);//逆置单词 if(*str==‘ ‘) str++; } return tmp; } int main() { char arr[]="i am a student"; printf("%s\n",reverse(arr)); return 0; }
时间: 2024-09-30 20:01:26