【问题标题】:replacing integers with strings in C用C中的字符串替换整数
【发布时间】:2018-05-11 07:06:37
【问题描述】:

我编写的代码用strings 替换了从03 的整数。我只被允许使用getchar()putchar()。如果input1,则输出将变为"one"

#include <stdio.h>

int main()
{
    int c;            
    char* arr[4] = {"zero", "one", "two","three"};
    int i;

        while ((c = getchar ()) != EOF) 
        {
            if(c==0+'0') {
                char* str = arr[0];
                for (i = 0; str[i] != '\0'; i++) {
                    putchar(str[i]);
                }
            }
            else if(c==1+'0') {
                char* str = arr[1];
                for (i= 0; str[i] != '\0';i++) {
                    putchar(str[i]);
                }
            }
            else if(c==2+'0') {
                char* str = arr[2];
                for (i = 0; str[i] != '\0'; i++) {
                    putchar(str[i]);
                }
            }
            else if(c==3+'0') {
                char* str = arr[3];
                for (i = 0; str[i] != '\0'; i++) {
                    putchar(str[i]);
                }   
            }
            else
                putchar(c);
   }

return 0;
}

code 很长。有没有更短的写法?

如果我输入33,输出将是"threethree"。谁能给我建议我如何修改我的代码不这样做?

注意:我也不能使用函数。

【问题讨论】:

    标签: c arrays getchar


    【解决方案1】:

    你可以用一个变量来记住最后的输入和比较,这样你就不会打印连续的字符了。

    #include <stdio.h>
    
    int main()
    {
        int c;
        char* arr[4] = {"zero", "one", "two","three"};
        int i;
        char last_input = '9';
    
        while ((c = getchar ()) != EOF)
        {
            if(c != last_input && '0' <= c  && c <= '3') {
                last_input = c;
                int index = c - '0';
                char* str = arr[index];
                for (i = 0; str[i] != '\0'; i++) {
                    putchar(str[i]);
                }
            }
            else{
                putchar(c);
            }
    
        }
        return 0;
    }
    

    【讨论】:

    • 谢谢,为什么最后输入的是'9'?
    • 如果输入为 1,它似乎只工作一次,输出将变为“一”,但如果我第二次输入,输出将为 1
    • 这不是你想要的问题吗?@makoto
    • 最后输入的'9'可以是0到3以外的任何值。我只是初始化它。也可以不初始化。
    • 抱歉,如果我输入 11,我应该更具体一些,然后输出是 oneone 而不是 11。
    【解决方案2】:

    您可以使用这样的一个 if 条件来压缩您的 if 语句:

    #include <stdio.h>
    
    int main()
    {
        int c;            
        char* arr[4] = {"zero", "one", "two","three"};
        int i;
    
        while ((c = getchar ()) != EOF) {
        int k = c-'0';
            if(k>=0 && k<=3) {
                char* str = arr[k];
                for (i= 0; str[i] != '\0';i++) {
                    putchar(str[i]);
                }
            }
            else {
                putchar(c);
            }
        }
    return 0;
    }
    

    【讨论】:

    • 您的代码将有Segmentation fault,因为'\n' 将通过getchar 得到,而'\n' - '0' 是-38,这会导致数组溢出。
    • 谢谢 我不太明白你为什么要减去 c-'0'
    • 如果我写 11 它将打印“oneone”
    • c-'0' 将给出我们要从中检索字符串的索引,
    • @makoto 11 应该给一个人吧?甚至您的代码也将 11 的输出作为 oneone
    【解决方案3】:

    这是完成相同任务的简单方法。我试图解释 cmets 中的逻辑。

    int main(void) {
            char *arr[11] = {"zero", "one", "two","three","four","five","six","seven","eight","Nine","Ten"};
            int *input = malloc(sizeof(*input))/*1st time 4 byte */ , row = 1;
            while( (input[row-1] = getchar())!=EOF ) {
    
                    if(input[row-1]==10) /* if ENTER key is presed */
                            break;
                    input[row-1] = input[row-1] - 48;/* convert it */
                    printf("%s ",arr[ input[row-1]%10 ]);/* its simple, just think on it */
                    row++;
                    input = realloc(input,row * sizeof(*input));/* reallocate based on number of input */
            }
            /* free dynamically allocated memory @TODO*/
            return 0;
    }
    

    我只是给出了提示,让它通用,比如输入小于零时写条件等。我希望它有所帮助。

    【讨论】:

    • 输入5417 并检查
    • 感谢您的回复,但我不能只使用 getchar 和 putchar 的 ise printf 或 scanf
    • 抱歉,我帮不上忙。无论如何Putchar() 意味着你没有在任何地方存储,只是在stdout 上打印然后就消失了。有什么用?
    • 无论如何在我的代码中代替print(),您都可以相应地替换为putchar()malloc() 是否允许使用?
    • no malloc 也是不允许的,但非常感谢您花时间帮助我
    【解决方案4】:

    这里我的代码使用循环来缩短你的代码。

    #include <stdio.h>
    
    int main()
    {
        int c;            
        char* arr[4] = {"zero", "one", "two","three"};
        int i, j;
    
        while ((c = getchar ()) != EOF) 
        {
            for(j = 0; j < 4; j++)
            {
                if(c == j + '0')
                {
                    char* str = arr[j];
                    for (i = 0; str[i] != '\0'; i++)
                    {
                        putchar(str[i]);
                    }
                    j = 10; // just to detect processed character
                    break;
                }
            }
            if(j != 10)
            {
                putchar(c);
            }
        }
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2011-10-20
      • 1970-01-01
      • 2019-05-09
      相关资源
      最近更新 更多