I am a students->students a am I
做事要动脑筋,要因情况而定。2016-07-23该
基本思路:
(1)单词单个反转
(2)整体反转
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<string.h>
-
void reverse(char *str,int begin,int end)
-
{//实现单词反转
-
char temp;
-
while (begin < end)
-
{
-
temp = str[begin];
-
str[begin++] = str[end];
-
str[end--] = temp;
-
}
-
}
-
void reverseString(char *str)
-
{
-
int length = strlen(str);
-
int begin = -1,end = -1;
-
int i = 0;
-
for (i = 0;i < length;i++)
-
{
-
if (begin == -1 && str[i] == ' ')//处理多个空格
-
continue;
-
if (begin == -1)//确定起始位置
-
{
-
begin = i;
-
continue;
-
}
-
if (str[i] == ' ')
-
{
-
end = i-1;
-
-
}
-
else if (i == length-1)
-
{
-
end = i;
-
}
-
else
-
{
-
continue;
-
}
-
reverse(str,begin,end);
-
begin =-1;
-
end = -1;
-
}
-
reverse(str,0,length-1);
-
}
-
int main()
-
{
-
char str[] = "I am a students.";
-
reverseString(str);
-
printf("%s\n",str);
-
return 0;
-
}
阅读(1203) | 评论(0) | 转发(0) |