-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;
}
3
  • Welcome to Stack Overflow. Please take the time to read The Tour and refer to the material from the Help Center what and how you can ask here. Commented Dec 28, 2016 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). At a minimum, you should [edit] your question to include a Minimal, Complete, and Verifiable example that reproduces your problem, along with the observations you made in the debugger. Commented Dec 28, 2016 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 Commented Dec 28, 2016 at 21:22

1 Answer 1

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.

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

4 Comments

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 jto be in range before using it for indexing into ara
and 3) Check the value of s[i] before indexing into ara
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.