0

this is a program to look into a word and see how many times a letter (input) is repeated

#include <stdio.h>
#include <string.h>

int main(){
    int cont = 0;
    char word[50], letter;
    int i;
    printf("enter the word: ");
    scanf("%s", word);
    printf("enter the letter you want to know how many times it repeats itself: ");
    scanf("%c", &letter);
    for (i=0; i < strlen(word); i++){
        if (letter == word[i])
        {
            cont += 1;
        }
    }
    printf("the letter you typed is repeated %d times", cont);
    return 0;
}

my issue is when it comes to compiling it, it only allows me to put my first scanf(word) and not my second one (letter).

I expected the terminal to ask me what letter I want to search but instead I got this.

(base) erickpruneda@MacBook-Air-de-Erick C % cd "/Users/erickpruneda/Documents/Documentos - MacBook Air de Erick/developer/code for s
chool/C/" && gcc strings.c -o strings && "/Users/erickpruneda/Documents/Documentos - MacBook Air de Erick/developer/code for school/C
/"strings
enter the word: book
enter the letter you want to know how many times it repeats itself: the letter you typed is repeated 0 times%  

as you can see, it didn't let me enter the letter. this was coded in VScode

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • The line `scanf("%c", &letter);` is reading the `\n` character that was left over by the line `scanf("%s", word);`. That is why the line `scanf("%c", &letter);` is not waiting for additional input. It does not need any additional input to match the `"%c"` format string, so it has no reason to wait for additional input. See the duplicate question for further information. – Andreas Wenzel Feb 22 '23 at 01:49
  • Hi Erick. This question is frequently asked here. Please try to first find a similar or equivalent question. Let us know if the duplicate question has an answer that helps you. – Iharob Al Asimi Feb 22 '23 at 01:52

0 Answers0