0

I'm in my first steps in C programming, and I came across a task I can not find a solution to.
The task is simple: taking a character from the user.
After that, you receiving a string from the user until the user types the * character.
Then print the number of times the user typed the first character.

I was able to solve the problem using char [SIZE]ת when I placed a maximum input size of 255 bytes (#define SIZE 255).
Nevertheless, my teacher tells me that although the solution is working well, this was not the purpose of the exercise, also, I can not assume a maximum string size.

He asks us to use the input buffer.
No dynamic memory allocation is used in the exercise, and only the stdio.h library is allowed.

I read a lot about the input buffer, but I still have not found the possibility to solve the exercise - how can I absorb value from the user without knowing its size?

I would be happy to receive assistance and tips on how to use the input buffer correctly.
Or more focused, how to input values (string of characters) into the input buffer, and then go over each character separately in this string and process it.

Thank You

1 Answer 1

1

There is no need to store all characters. Once you have read a character you can throw it away and just increase a counter. Something like this:

#include <stdio.h>

int main() {
    char c, first;
    int counter=0;
    printf("Enter first character: ");
    scanf("%c", &first);
    do {
        scanf("%c", &c);
        if(c == first)
            counter++;
    } while (c != '*');
    printf("You entered '%c' %d times\n", first, counter);
}

Output:

Enter first character: a
aaaaa*
5

or

Enter first character: a
aabbaa*
You entered 'a' 4 times

Note:

As been pointed out in the comments, scanf is not a good tool for this kind of stuff. I would advice against the usage of it, unless you know it is the right tool. But that's beside the point. The point here was to show you that you don't need to store the whole input buffer. If you want to look at alternate input methods (as William Pursell suggested in the comments) you could have a look at fgetc, getc, or getchar for reading single characters. fread is also a tool you should get familiar with.

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

7 Comments

but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
@JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
@Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
@WilliamPursell Which way do you recommend a beginner to get and read a single character? And sorry to raise the subject again - I still did not understand how we perceive a whole string of characters into a character-size variable.
|

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.