0

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.

#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"
(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.

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:

The son's name (composed by 10 characters, add the special character $ if needed).

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).

This will represent a family of 3 people. 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).

Call this function on the whole family members.

This is the input exemple:

Roberta$$$Anna$$$$$$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.

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?

3
  • 1
    Read the documentation of scanf() it might help you. Afterwards read sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html to learn about how it might hurt you. Commented Jul 8, 2018 at 16:23
  • For storing three-letter-strings like you attempt to use, you need arrays of four characters each, becaue of the zero needed at the end. Commented Jul 8, 2018 at 16:25
  • Okay, thanks. I read about the fact that char arrays needs one more space for the \0 but the exercise is about three arrays of size ten and the input is about 30 characters in a single line, so I ll' have to find a workaround. Thanks to everyone. Commented Jul 8, 2018 at 16:46

1 Answer 1

0

Hey Snoopy3 Stack overflow is not a platform to get your home works done. 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().

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer. I know that stackoverflow is not meant to solve my work, i wrote the entire exercise because in other users' answers I was told to change the arrays' lenght, so I just wanted to show that the exercise didn't allow me to do so (that's why I wrote at the end of my post to ignore the first part of the exercise)
This has undefined behavior with scanf("%10s%10s%10s", a, b, c);, overwriting the ends of the character arrays with '\0' terminators.
@DavidBowling overwriting the ends of the character arrays with '\0' terminators was done to reduce computation complexity.
@anoopknr -- it is not a good strategy to simplify code by invoking undefined behavior. The %10s directive tells scanf() to write up to 10 characters into an array, here an array that can contain at most 10 characters. Then scanf() always writes a null terminator when writing to a string, thus writing the character to, e.g. a[10], if the input name is 10 characters long (a possibility according to OP). This writing out of bounds may appear to work, but it causes undefined behavior. If OP wants to get good marks on the assignment, they won't turn in code with UB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.