简体   繁体   English

在 C 中扫描一个字符串和 printf

[英]scanf a string and printf it in C

What I can do to do this?我能做些什么来做到这一点?

#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);
}

I am not saying this is good code, but I "think" this is kind of what you are trying to do.我并不是说这是好的代码,但我“认为”这是您正在尝试做的事情。 Maybe it will give you an idea so you can fix your code the way you want it.也许它会给你一个想法,这样你就可以按照你想要的方式修复你的代码。

    #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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM