输入: 你好
输出:
奥莱赫特
输入:
你好吗?
输出:
Woh时代?你
#include <stdio.h>
#include <string.h>
int main()
{
char s[100], r[100];
int n, c, d=0;
int p=0,t=0;
printf("Input a string\n");
gets(s);
//n = strlen(s);
while(s[p]!= '\0'){
while(s[p]!= ' ' || s[p]!= '\0'){
p++;
}
for (c = p-1; c >= t; c--, d++)
r[d] = s[c];
r[d++] = ' ';
// printf("%s\n", r);
t=p;
p++;
}
r[d]= '\0';
printf("%s\n", r);
return 0;
}
我遇到了超出时间限制的问题。不知道我哪里出了问题。请帮助我解决问题。
最佳答案
我认为内部 while 循环永远不会终止,因为条件永远不会给出 false。 要终止循环:
s[p] != ' ' 和 s[p] != '\0' 都应该为 false。
也就是说
s[p]== ' ' 和 s[p]== '\0' 都应该为 true,但这是不可能的。
这就是您收到超出时间限制错误的原因。
关于将字符串转换为特殊字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33721082/