将字符串逆序,比如输入abcd,返回dcba
void reverse(char*left,char *right) {
while (right>left) {
char temp = *left;
*left = *right;
*right = temp;
right--;
left++;
}
}
int main() {
char arr[100] = { 0 };//定义一个字符数组,用来存储输入的字符串
gets(arr);//I like Beijing.
int length = strlen(arr);
reverse(arr,arr+length-1);//.gnijieB ekil I
char* start;
char* end;
start = arr;
end = start;
while (*start)//只要开始字符不为空字符就继续
{
end = start;
while ((*end != ' ') && (*end != '\0'))
{
end++;
}
reverse(start, end - 1);
if (*end != '\0')
end++;//跳过空格
start = end;
}
//遍历最终结果
int i = 0;
for (i = 0; i < length; i++) {
printf("%c", arr[i]);
}
}