-4

Actually recently i found a problem where i need to count occurrence a given(by oj) char in a given string(with test case).So i write this code but output is not as i desired.I'm a beginner so i'll be greatly thankful for any kind of instructive advice or help.THANK YOU.

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

int main ()
{
    int ara [123];
    char s[1000];
    int l, j, i, len;
    char c;

    scanf ("%d\n", &l);

    while (l >= 0){

        for (i = 65; i <= 122; i++)
        {
            ara[i] = 0;
        }

        fgets(s, 1000, stdin);
        len = strlen(s);

        for (i = 0;i <= len; i++)
        {
            ara[s[i]]++;
        }

        scanf(" %c\n", &c);
        j = c;
        printf("count : %d\n", ara[j]);

        l--;
    }

    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Rifath
  • 21
  • 1
  • 2
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Dec 28 '16 at 14:44
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Dec 28 '16 at 14:45
  • If any of the posted answers solve your problem, please remember to accept it. That help us all to know that no further action is needed – Support Ukraine Dec 28 '16 at 21:22

1 Answers1

1

The problem is that scanf is leaving a newline in the input to be read as the target sentence.

You can get around this by using fgets and sscanf instead. I also added some cues to make it easier to know what is expected.

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

int main (void)
{
    int ara [128];                          // increased array size
    char s[1000];
    char t[10];
    int l, j, i, len;
    char c;

    printf("Enter how many loops:\n");
    fgets(t, sizeof t, stdin);              // replace scanf
    sscanf (t, "%d\n", &l);                 // with sscanf

    while (l > 0){                          // changed end test

        for (i = 'A'; i <= 'z'; i++)        // replaced magic numbers
        {
            ara[i] = 0;
        }

        printf("Enter the string:\n");
        fgets(s, sizeof s, stdin);
        len = strlen(s);

        for (i = 0;i <= len; i++)
        {
            ara[s[i]]++;
        }

        printf("Enter the letter:\n");
        fgets(t, sizeof t, stdin);          // replace scanf
        sscanf (t, "%c\n", &c);             // with sscanf
        j = c;
        printf("count : %d\n", ara[j]);

        l--;
    }

    return 0;
}

Program session

Enter how many loops:
2
Enter the string:
one two three four
Enter the letter:
o
count : 3
Enter the string:
three seven
Enter the letter:
e
count : 4

Note that ideally, you should also check the function return values from fgets and sscanf.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 1
    I was writing my own answer when this answer was posted. So now I don't see any reason to post mine as the principle was the same. But there are a few things I think should be added: 1) Initialize the whole `ara` array. Currently this code has UB. The extra time it takes to initialize everything doesn't matter in a program reading from stdin. 2) Check the value of `j`to be in range before using it for indexing into `ara` – Support Ukraine Dec 28 '16 at 15:30
  • 1
    and 3) Check the value of `s[i]` before indexing into `ara` – Support Ukraine Dec 28 '16 at 15:36
  • 1
    @4386427 thank you, I only mentioned error checking at the end. The array could have had 256 elements, and with `unsigned char` input. There were some other things I could have mentioned but wanted to keep the answer close to the question, not make an exhaustive rewrite. – Weather Vane Dec 28 '16 at 16:04
  • 1
    Agree - sometimes it is better to focus on the main problem and let minor issues pass. I just wanted to give OP a chance to pick up some additional feedback. +1 from here. – Support Ukraine Dec 28 '16 at 20:50