【问题标题】:how to display entered letter in c?如何在c中显示输入的字母?
【发布时间】:2014-03-13 06:25:59
【问题描述】:

我是c programming 的新手。我已经为输入的字母创建了一个程序,最后显示了输入的字母..但它总是只显示最后的字母..请帮助..我知道它的简单问题,但我是初学者,所以请帮助大家..

 #include<stdio.h>      
    int main()
    {
        char z;
        int a;
            printf("enter the no.");
            scanf("%d",&a);
            printf("the entered no. is:%d\n",a);
            int i;
            for(i=0;i<a;i++)
            {
                printf("enter the letters:");
                scanf("%s",&z);
            }
            printf("the entered letters are:");
            for(i=0;i<a;i++)
            {
                printf("%s\n",&z);
            }
            return 0;

    }

【问题讨论】:

  • 谢谢艾西瓦娅 karthik!

标签: c for-loop


【解决方案1】:

问题:

  1. 您应该使用%c(用于字符)而不是%s(用于字符串)。

  2. 使用字符数组存储多个字符。阅读数组here

  3. 在第二个for 循环中从printf() 中删除&amp;

试试这个:

int main()
{
    char z[10];  //can hold 10 characters like z[0],z[1],z[2],..
    int a;
    printf("enter the no.");
    scanf("%d",&a);
    printf("the entered no. is:%d\n",a);
    int i;
    for(i=0;i<a;i++)
    {
        printf("enter the letters:");
        scanf("%c",&z[i]);
    }
    printf("the entered letters are:");
    for(i=0;i<a;i++)
    {
        printf("%c\n",z[i]);
    }
    return 0;
}

【讨论】:

  • 谢谢兄弟,但我只想打印输入的字母..它显示了所有像三角形这样的字母
  • 该数组最多只能包含 10 个字符。如果您想要更多,请更改数组大小。 it displayed all the letters like triangle?我没听懂。
  • 如果我输入了 a b c d e f g h i 之类的字母,则输出类似于 abcdefghi abcdefgh abcdefg abcdef abcde 但逐行输入。
  • 首先,设置限制(a 的值 scanf 将停止。然后在下一个for 循环中,它将打印您输入的每个字母。
  • 请编辑我的代码兄弟,直到我没有任何解决方案
【解决方案2】:

请查看this 了解有关 scanf 的更多详细信息。你给了scanf("%s",&amp;z); %s 用于读取字符串(除换行符之外的字符数组并以null 字符结尾)。所以如果你把这个放在循环里面,你不会得到想要的结果。如果您想一次只读取一个字符,请在此处使用 %c 表示字符。

for(i=0;i<a;i++)
{
            printf("enter the letters:");
            scanf("%c",z+i);
}

【讨论】:

    【解决方案3】:

    char z 是仅用于一个字符的占位符。而且您在for 循环中将设置z 的内容写完了。要接收更多字符,请使用其他人提到的char array

    或者在你扫描它们的循环中打印字符:

    #include<stdio.h>      
    int main()
    {
        char z;
        int a;
        printf("enter the no.");
        scanf("%d",&a);
        printf("the entered no. is:%d\n",a);
        int i;
        for(i=0;i<a;i++)
        {
             printf("enter the letters:");
             scanf("%s",&z);
             printf("letter scanned:%c\n", z);
        }
        return 0;
    }
    

    【讨论】:

    • 请编辑我的代码兄弟,直到我没有任何解决方案
    • 朋友它不对..我会清楚地解释我想要什么..如果我将输入作为 4 之后将 4 传递给 for 循环并要求输入 4 个字母.. 在我之后输入四个字母..它将显示新行..但你的代码是如果我gv a,它一次显示一个..但我不想像那个朋友..你能理解我的话吗?​​
    • 是的,我愿意。如果您清楚地阅读了我的答案,则说明您无法使用 char 变量做您想做的事情。要么像其他人建议的那样使用array,要么在阅读变量时将变量打印为&。希望你现在有更好的理解。
    【解决方案4】:

    使用%c 扫描字母。要扫描多个字母,您可以使用 char 数组:char z[10];

    你想要做的事情可以通过这种方式完成:

            char z[10];   // Take some max size array
            ...
            for(i=0;i<a;i++)
            {
                printf("enter the letters:");
                scanf("%c",&z[i]);    // Scan the letters on each array position.
            }
            printf("the entered letters are:");
            for(i=0;i<a;i++)
            {
                printf("%c\n",z[i]);  //'printf' doesn't require address of arg as argument hence no `&` required
            }
    

    %s 参数用于扫描chars 的字符串。

    注意charsstringcharsarray 之间的区别。 C中chars的字符串需要以char格式的ASCII字符0表示为\0,而char数组只是字母的集合,不需要以\0结尾。

    当您尝试对printfstrcpystrlen 等字符串执行某些操作时,差异变得更加重要。这些函数适用于字符串的空字符终止属性。

    例如:strlen 对字符串中的字符进行计数,直到找到\0,以找出字符串的长度。同样,printf 逐个字符地打印字符串,直到找到 \0 字符。

    更新:

    忘了提到scanf 不是输入char 格式的好选择。请改用fgetc,将stdin 作为输入文件流。

    【讨论】:

    • 请编辑我的代码兄弟,直到我没有任何解决方案
    【解决方案5】:
    #include <stdio.h>      
    
    int main()
    {
        char *z;
        int a;
        printf("enter the no.");
        scanf("%d",&a);
        z = (char *) malloc(a);
        printf("the entered no. is:%d\n",a);
        int i;
        for(i=0;i<a;i++)
        {
            printf("enter the letters:");
            scanf("%c",z+i);
        }
        printf("the entered letters are:");
        for(i=0;i<a;i++)
        {
            printf("%c\n",z);
        }
        return 0;
    }
    

    【讨论】:

    • 请编辑我的代码兄弟,直到我没有任何解决方案
    【解决方案6】:

    你的代码中的第一个错误是你使用了“%s”而不是“%c”。第二个是不可能在一个变量中存储多个值,所以不要使用变量使用数组。第三个是你已经告诉用户输入他/她想输入的你不知道的字符数。他们也可以输入 1 和 100000 也可以不定义数组中的成员数。更好的是使用数组中的特定字符数.

    【讨论】:

      【解决方案7】:

      我终于得到了答案,谢谢你的帮助,stackoverflow 伙计们简直摇摇欲坠......

      #include<stdio.h>
      #include<malloc.h>
      int main()
      {
              int a;
              char *z=(char *)malloc(sizeof(a));  
              printf("enter the no.");
              scanf("%d",&a);
              printf("the entered no. is:%d\n",a);
              int i;
              for(i=0;i<a;i++)
              {
                  printf("enter the letters:");
                  scanf("%s",&z[i]);
              }
              printf("the entered letters are:\n");
              for(i=0;i<a;i++)
              {                
                  printf("%c\n",z[i]);
              }
              return 0;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        • 2023-02-03
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多