【问题标题】:scanf a string and printf it in C扫描一个字符串并在C中打印它
【发布时间】:2020-07-27 14:35:51
【问题描述】:

我能做些什么来做到这一点?

#include <stdio.h>
#include <string.h>

void lee(char s[]);
void escribe(char s[]);

int main()
{
    char str[20];
    char ch;

    printf("1 escribe\n2 lee\n");
    ch=getchar();
    switch(ch) {
        case '1': 
            printf("you are in escribe (write)\n");
            lee(str);
            break;
        case '2': 
            printf("you are in lee (read)\n");
            escribe(str);            
            break;
        default: puts("Error");
    }


    return 0;
}

void lee(char s[]){
    printf("write your sentense\n");
    scanf("%[^\n]", s);
}

void escribe(char s[]){
    printf("Entered string is %s \n", s);
}

【问题讨论】:

  • 定义“不起作用”。发生什么了?应该怎么办?
  • 当我去李时,我不会写
  • 不要在您的问题中添加各种恐慌的“帮助我 plzzzzz”文本。相反,停下来,深呼吸,然后更详细地解释问题所在。
  • 首先我建议你清理你的代码。您的 printf 说了一件事,并且调用的方法位于错误的位置。包括清晰的描述你想要的结果。

标签: c string printf scanf


【解决方案1】:

我并不是说这是好的代码,但我“认为”这就是您正在尝试做的事情。 也许它会给你一个想法,这样你就可以按照你想要的方式修复你的代码。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    void lee(char s[]);
    void escribe(char s[]);

    int main()
    {
        char str[20] = {0};
        char ch = '\0';


        while(1)
        {
            printf("\n1 escribe:\n2 lee:\n");
            ch = getchar();
            fseek(stdin, 0, 0);

            switch(ch) {
                case '1' :
                    printf("\nYou are in escribe (write)\n\n");
                    lee(str);
                    break;

                case '2' :
                    printf("\nYou are in lee (read)\n\n");
                    escribe(str);
                    break;

                default: puts("Error");
                    exit(1);
            }
        }
    return 0;
    }

void lee(char s[]){
    printf("Write your sentence: \n");
    scanf("%19[^\n]", s);
    fseek(stdin, 0,0);
    printf("\n");
}

void escribe(char s[]){
    printf("The entered string is: %s ", s);
    printf("\n");
}

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多