简体   繁体   English

C中单个文本字符串中的多个char数组scanf

[英]multiple char arrays scanf in a single string of text in C

For an exercise in C, I need to put three names in three different arrays of char but the input of the three names is on a single line. 对于C语言的练习,我需要将三个名称放入三个不同的char数组中,但是三个名称的输入在同一行上。

#include <stdio.h>

int main(){
    char a[3], b[3], c[3];

    scanf("%s%s%s", a, b, c);
    printf(" \n %s ", a);
    printf("%s ", b);
    printf("%s", c);

    return 0;
}

The input will be in a single line like this: "aaabbbccc" 输入将在单行中,如下所示:“ aaabbbccc”
(without " "). (不包含“”)。

I tried with that but it require to press enter three times. 我尝试过,但是需要按回车三下。

Is there a way to scan more than one array with a single line of input? 有没有一种方法可以用单行输入来扫描多个阵列?

I can only use stdio.h and scanf. 我只能使用stdio.h和scanf。

edit: 编辑:

This is the full exercise translated (the original one was in italian). 这是完整的练习翻译(原始练习是意大利语)。

Copy those declaration at the top of your code. 将这些声明复制到代码的顶部。

struct person {
    char name[10];
    struct person * mother;
    struct person * father;
} ;
typedef struct person Person;

write a code that declares three Person type variables and reads from input: 编写一个声明三个Person类型变量并从输入中读取的代码:

The son's name (composed by 10 characters, add the special character $ if needed). 儿子的名字(由10个字符组成,必要时添加特殊字符$)。

The mother's name (same as the son's). 母亲的名字(与儿子的名字相同)。

The fater's name (same as the son's). 胖子的名字(与儿子的名字相同)。

(use NULL constant if father and mother are unknown). (如果父亲和母亲未知,则使用NULL常量)。

This will represent a family of 3 people. 这将代表一个3人的家庭。 Then write a void function that prints a Person's mother and father s' name. 然后编写一个虚函数,打印一个人的父母的名字。 If those names are unknown then print "Unknown" (check that the pointer is different from NULL). 如果这些名称未知,则打印“未知”(检查指针是否不同于NULL)。

Call this function on the whole family members. 在整个家庭成员上调用此函数。

This is the input exemple: 这是输入示例:

Roberta$$$Anna$$$$$$Massimo$$$ 罗伯塔$$$安娜$$$$$ Massimo $$$

Ignoring the last part of the exercise, my problem is that the input is made by 30 characters, and the name array length is 10. I obviously can't edit name array's length. 忽略练习的最后一部分,我的问题是输入由30个字符组成,并且名称数组的长度为10。我显然不能编辑名称数组的长度。

edit: I tried with the "%3s" solution on the three lenght arrays and or seems ti work fine, but, if there is a '\\0' character on the 3rd slot of the array, this shouldn't be anche issue? 编辑:我尝试在三个长度较大的数组上使用“%3s”解决方案,或者看起来可以正常工作,但是,如果数组的第三个插槽上有一个'\\ 0'字符,这应该不是问题吗?

Hey Snoopy3 Stack overflow is not a platform to get your home works done. 嘿,Snoopy3 Stack overflow不是完成家庭作业的平台。 Here I can only help you by modifying your code. 在这里,我只能通过修改代码来帮助您。

You can have multiple char arrays scanf() in a single string of text in C using format specifier in scanf() . 您可以使用scanf()中的格式说明符在C中的单个文本字符串中具有多个char数组scanf()

Try this code :- 试试这个代码:-

#include <stdio.h>

int main()
{
  char a[10], b[10], c[10];

  scanf("%10s%10s%10s", a, b, c);   // total 30 chars 10 per each string.

  for (int i = 0; i < 10; i++)      // removing rest '$';
  {
    if (a[i] == '$')
    {
      a[i] = '\0';
    }
    if (b[i] == '$')
    {
      b[i] = '\0';
    }
    if (c[i] == '$')
    {
      c[i] = '\0';
    }
  }

  printf(" \n %s ", a);
  printf("%s ", b);
  printf("%s", c);

  return 0;
}

Output :- 输出:-

Roberta$$$Anna$$$$$$Massimo$$$

 Roberta Anna Massimo

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

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