【问题标题】:Entering 3 letters in array with space between it在数组中输入 3 个字母,其间有空格
【发布时间】:2016-04-20 16:47:39
【问题描述】:

我需要在 char 数组上输入 3 个字母或数字,它们之间有空格。我需要使用 scanf,因为我们没有学习任何其他方法。这是我的代码:

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

    void main(){

        char lista[3];
        int x;
        printf("Enter 3 letters with space between them : ");
        scanf_s(" %s", lista, sizeof(lista));
    }

【问题讨论】:

  • 您有问题吗?
  • @AntonioGvardijan 你需要这个scanf_s(" %c %c %c", &amp;lista[0],(rsize_t)1,&amp;lista[1],(rsize_t)1, &amp;lista[2],(rsize_t)1);
  • 发布输入和预期输出的示例会有所帮助。缺乏细节。这些都好吗? "a b c", "a b 10", "a b (many spaces) c", "ab c", "a á c", "a b c d"?根据某种定义,这些都符合“字符数组上的 3 个字母或数字”

标签: c arrays char


【解决方案1】:
char c[3];
    int count = 0;

    printf("Enter 3 chars separated by space and press enter :");
    while (count < 3)
    {
        scanf("%c%*[ ]", &c[count]); // *[ ] ignores the empty spaces after each character
        count++;
    }

    printf("c1:%c\n", c[0]);
    printf("c2:%c\n", c[1]);
    printf("c3:%c\n", c[2]);

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2018-05-17
    • 2022-01-05
    • 1970-01-01
    相关资源
    最近更新 更多