扫描字符串并在C中打印它

问题描述 投票:-1回答:2

我该怎么做?

#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);
}
c string printf scanf
2个回答
0
投票

读取here

要读取字符串,请执行以下操作之一:

#include <stdio.h>

int main ()
{
  char str [80];
  int i;

  printf ("Enter your family name: ");
  scanf ("%79s",str);  
  printf ("Enter your age: ");
  scanf ("%d",&i);
  printf ("Mr. %s , %d years old.\n",str,i);
  printf ("Enter a hexadecimal number: ");
  scanf ("%x",&i);
  printf ("You have entered %#x (%d).\n",i,i);

  return 0;
}

0
投票

我并不是说这是好的代码,但是我“认为”这是您要尝试执行的操作。也许它将为您提供一个想法,以便您可以按自己的方式修复代码。

    #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("%[^\n]", s);
    fseek(stdin, 0,0);
    printf("\n");
}

void escribe(char s[]){
    printf("The entered string is: %s ", s);
    printf("\n");
}
© www.soinside.com 2019 - 2024. All rights reserved.