Hello folks,
I have a question again: This time I tried to built a C project for university. The user should prompt a single word into the command line. Before, the user is told to do so by a statement which is printed into the command line using the printf function. After that the user is requested to insert a letter by another printf statement. Then the programm wants the user the to type in letter by calling the scanf function for a second time.
In the end, a for loop counts the times the letter appears in the given word. Finally it prints out the number which was created by the loop.
The problem is if I typed in the word, the system requests me to type in the letter directly by easily skipping the printf.
After I typed in the letter, the second printf statement appears and I have to type the letter a second time.
I already tried the flush method, but it not solved the problem.
Does anyone know the solution to this answer? I have listed the code below.
Thank you!
#include <stdio.h>
int main() {
char word[5];
char letter;
int count = 0;
printf("Please type in a word.\n");
scanf("%s\n", word);
printf("Please type in a letter.\n");
scanf("%c\n", &letter);
for (int i = 1; i <= 20; i++) {
if (word[i] == letter) {
count++;
}
}
printf("The letter %c is used %d times.\n", letter, count);
return 0;
}