-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;
}
5
  • 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. Commented Nov 14, 2017 at 5:20
  • scanf("%s", &input[i]); -> scanf("%99s", input[i]); Commented Nov 14, 2017 at 5: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. Commented Nov 14, 2017 at 5: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 minimal reproducible example!" Commented Nov 14, 2017 at 7:21
  • Your question is lacking the exact input/output that you're feeding into the program and receiving from it. Commented Nov 14, 2017 at 7:22

3 Answers 3

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().

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

Comments

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;
}

1 Comment

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().
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.