将字符串转换为特殊字符串

标签 c string

输入: 你好

输出:

奥莱赫特

输入:

你好吗?

输出:

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/

相关文章:

c - 为什么 pthread_join() 从未被调用?

c - 修改控制台 C Codeblocks 中先前编写的行

.net - 字符串类型是存储在堆上还是堆栈上?

c - 如何将客户端和服务器链接到同一个信号量

c - Typedef 到双整数数组

c - 在 Racket 中,我可以在调用另一个函数后导出函数吗?

Java:将字符串转换为 Swing 表中的代码

javascript - 获取括号内的文本

regex - 意外 token ";;"ocaml

c# - string.replace 不工作