0

I have to enter chars in C. Input is of the form:

 'f'  'g'
 'd'  'b'
 '2'  '3'

Each line has a pair of two characters, and my task is to store them in two different arrays. One array will store {'f','d','2'} and the other will store {'g','b','3'}.

My code is this:

 for(i=0;i<n;i++){ 
   scanf(" %c",&arr1[i]); 
   scanf("%c",&arr2[i]);
 }

where n is the no. of rows.

There is no error but no values are stored in the second array.

2
  • Is you input exactly as you have shown to us? With all the 's and all? Or is it rather like, fg then a new-line, then db, and so on? Commented Jul 1, 2014 at 17:36
  • its like f and then enter and g and then enter and so on.. Commented Jul 1, 2014 at 17:41

3 Answers 3

2

Your code looks fine except you need space before %c in second scanf too:

for(i=0;i<n;i++){ 
   scanf(" %c",&arr1[i]); 
   scanf(" %c",&arr2[i]);
}  

A space before %c in scanf can skip any number of white-spaces. In absence of it, second scanf reads the white-spaces and store that in arr2.

==> Live Demo.

Sign up to request clarification or add additional context in comments.

4 Comments

@user3794823 I added the explanation to my answer.
i understand your point but what i am doing is i after every symbol i am pressing enter without any spaces. so in that situation why do i need spaces.
@user3794823 When you press enter a new line is put in which is also white space.
@user3794823; I explained this here few hours ago. You need to consume \n in that case.
1

If your input is f, you press enter, and then g - then your second scanf call will consume the new line between the letters.

scanf("%c", ... Will read next character (this includes white space so it may read blank character). scanf(" %c", ... Will skip white-space and read in next non-white-space character.

Note: New lines ('\n') are also considered white-space.

Comments

0

I mean, since you've told us that the input is of form like you've shown to us, there apparently are 's and spaces to be discarded. If so, it is almost as if you have to parse an input...

If you are certain that the input will be of the form you've described, then one way to do it would be to:

  • Read and discard until the first single-quote, then also the single-quote
  • Read and store the following character
  • Read and discard the following single-quote
  • Do this once again

On each loop. What I have described above, could be achieved with the following block consisting of a single scanf call, where the format-string is the important part:

for ( ... ) {
    scanf( "%*[^']'%c'%*[^']'%c'", &arr1[i], &arr2[i] );
}

This assumes that the input will be of the form as displayed on the question, with a single-character inside single-quotes, arbitrarily spaced, and like that.

  • %*[^']' part handles the first bullet on the list above
  • %c and the paired &arr1[i] does the second
  • ' does the third
  • Rest does the fourth

I know that, as per asker's comment, that this is not what's being looked for, however I felt like there should at least be an answer addressing what the question says and not just what the asker thinks, with the hope that it helps anybody.

Comments