-1

I am trying to get multiple words input and multiple lines into one array. But somewhere the code skips getting the input and skips to end the program. I have tried adding space before and after the ' %s' (or '%s ') but it won't work (maybe because it's inside a loop?). Would really appreciate anyone's help! It also starts to act weird if I enter more than two-three words :( My goal is to find how many times a specific letter has occurred throughout all those words and lines.

#include <stdio.h> //include standard library

int main(){
  int lineCount, occuranceCount;
  printf("How many lines are you going to enter?");
  scanf("%d", &lineCount);

  char input[lineCount][100], searchChar;

  for(int i=0; i<lineCount; i++){
    printf("Please enter line #%d (100 characters of less):",i+1);
    scanf("%s", &input[i]);
  }

  printf("What letter do you want to check the frequence in those lines? ");
  scanf("%c", &searchChar);

  for(int j=0; j<lineCount; j++){
    for(int k=0; k<100; k++){
      if(input[j][k] != '\0'){
        if(input[j][k]==searchChar)
          occuranceCount++;
      }
    }
  }

  printf("The letter occurs for %d time", occuranceCount);

  return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
arthrax
  • 161
  • 2
  • 4
  • 12
  • 4
    Which is one of the primary reasons why `fgets` (or POSIX `getline`) are recommended for user input instead of `scanf` which is full of pitfalls for the unwary... Regardless which input function you use, you must ***validate the return*** to have any confidence you will not invoke *Undefined Behavior* on your next attempt to access what you attempted to read. – David C. Rankin Nov 14 '17 at 05:20
  • `scanf("%s", &input[i]);` -> `scanf("%99s", input[i]);` – Ed Heal Nov 14 '17 at 05:22
  • 1
    A summary of why it skps? (1) you read with `scanf ("%d"...)` which leaves the `'\n'` (from the user pressing *Enter* in `stdin`. (2) you call `scanf ("%s"...)` which reads nothing because the `"%s"` *format specifier* reads from the beginning up to the first *whitepace* (`'\n'` being whitespace) which is not removed from `stdin`; (3) you call `scanf ("%c"...)` which happily accepts the `'\n'` as a *character* for its input and (4) you fail to check the return of all of the calls. – David C. Rankin Nov 14 '17 at 05:27
  • "Questions seeking **debugging help ("why isn't this code working?")** must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]!" – Antti Haapala -- Слава Україні Nov 14 '17 at 07:21
  • Your question is lacking the exact *input/output* that you're feeding into the program and receiving from it. – Antti Haapala -- Слава Україні Nov 14 '17 at 07:22

3 Answers3

7
  scanf(" %c", &searchChar);
         ^

You need the space over here to consume any \n from stdin.

Also scanf() will read one word not a line(space separated words) as you are thinking.

And also it is better to use strlen(input[j]) to know how much you should read.

Another thing, use size_t instead of int in the looping.

Intialize occuranceCount to 0.

Also to avoid buffer overrun exploits use scanf("%99s", input[i]); in your code.

In order to read a line you can use fgets().

user2736738
  • 30,591
  • 5
  • 42
  • 56
4

1) Change

  scanf("%c", &searchChar); -->   scanf(" %c", &searchChar);

to get rid of any \n left in the input buffer from previous scanf

2) Change

for(int k=0; k<100; k++){ --> for(int k=0; k<strlen(input[j]); k++){

to avoid reading beyond the actual user input.

Besides that:

Never do scanf("%s", &input[i]); as the user can overflow your input buffer. At least change it to: scanf("%99s", &input[i]); but consider using fgets instead

Using fgets your program could be:

#include <stdio.h> 

int main(){
  size_t lineCount = 0, occuranceCount = 0;
  printf("How many lines are you going to enter?");
  scanf("%zu ", &lineCount);
  if (lineCount == 0) return 0;  // Input error

  char input[lineCount][100], searchChar;

  for(size_t i=0; i<lineCount; i++){
    printf("Please enter line #%zu (100 characters of less):",i+1);
    fgets(input[i], 100, stdin);
  }

  printf("What letter do you want to check the frequence in those lines? ");
  scanf("%c", &searchChar);

  for(size_t j=0; j<lineCount; j++){
    for(size_t k=0; k<strlen(input[j]); k++){
        if(input[j][k]==searchChar) occuranceCount++;
    }
  }

  printf("The letter occurs for %zu time", occuranceCount);

  return 0;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • `scanf("%zu ", &lineCount);` does not return until a line of non-white-space text is entered _after_ the line count. Mixing `scanf()` with `fgets()` is challenging. Better to only use `fgets()`. – chux - Reinstate Monica Nov 14 '17 at 14:55
2

scanf using the %s always read until a ' '(whitespace) or '\n'(new line) is encountered, so you always can read only one word with scanf("%s", s1)...

if you want to read the spaces or new line characters you must use gets, or fgets(more secure than gets)