标签:The amp 空间 clu char lib str turn 内容
eg: student a am i
i ma a tneduts
i am a student
代码具体思想
1.将句子反转(不管单词拼写顺序)
2.将各个单词分别反转为正确顺序
源代码:
#include<stdio.h>
#include<stdlib.h>
void Reverse(char *left, char *right)//范围反转
{
char temp;
while (left < right)
{
temp = *left;
*left = *right;
*right = temp;
left++; right--;
}
}
int TheStrlen(char *str)。。求字符串长度
{
int count=0,i=0;
while (*(str + i)!=‘\0‘)
{
count++;
i++;
}
return count;
}
void ReverseEnd(char *str)
{
char *left = str;
char *right = str + TheStrlen(str) - 1;
char *p = str;
char *sta;
Reverse(left, right);
while (*p != ‘\0‘)//将具体单词反转
{
sta = p;
while (*p != ‘ ‘&&*p != ‘\0‘)
{
p++;
}//测单词长度范围
Reverse(sta, p - 1);//反转单词
if (*p == ‘ ‘)//跳转至下一个单词
{
p++;
}
}
}
int main()
{
char str[] = "i am a student";
ReverseEnd(str);
printf("%s\n", str);
system("pause");
return 0;
}
标签:The amp 空间 clu char lib str turn 内容
原文地址:https://blog.51cto.com/14232799/2382186