#include <stdio.h>
#include <string.h>
/*翻转字符串*/
void reverseStr(char *str, int nStart, int nEnd)
{
if(NULL == str || '\0' == *str || nStart >= nEnd)
return;
while(nStart < nEnd)
{
char cTemp = str[nStart];
str[nStart] = str[nEnd];
str[nEnd] = cTemp;
nStart++;
nEnd--;
}
}
/*翻转句子*/
void reverseWords(char *str)
{
int begin, end;
begin = end = 0;
char *strTmp = str;
reverseStr(str, begin, strlen(str) - 1);
while(*str != '\0')
{
if(*str == ' ')
{
reverseStr(strTmp, begin, end - 1);
begin = end + 1;
}
end++;
str++;
}
/*末尾的单词处理*/
reverseStr(strTmp , begin, end - 1);
}
int main(void)
{
int nStart, nEnd;
char str[] = "Here is a test";
printf("pre reverse : %s\n", str);
reverseWords(str);
printf("after reverse : %s\n", str);
return 0;
}
/*
打印
pre reverse : Here is a test
after reverse : test a is Here
*/
单词翻转
最新推荐文章于 2023-12-17 18:55:00 发布