-1

I am trying to count how many times a character appears in a string. I want to count with a for loop for each character that has another loop that iterates through every character of the string. I am not sure what is wrong with it or if it is even possible to do it that way. I have seen other ways it can be done, but I was wondering if I am close to achieving it or my code is entirely faulty.

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

int main()
{
    char A[100], ch;
    int cnt[100]={};
    gets(A);
    int x = strlen(A)-1;
    int i=0;

    for(ch='a';ch<='z';ch++)
    {
        for(i = 0; i <= x; i++)
        {
            if(A[i]==ch)
                cnt[i]++;
        }
        printf("%c is %d times in string\n", ch, cnt[i]);
    }
    return 0;
}

enter code here

Greggs
  • 1
  • 4
  • 1
    Never ***ever*** use `gets`. It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it has been obsolete since the mid 1990's, and was removed from the language altogether in the C11 specification, ten years ago. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. And don't forget to check what it *returns*. – Some programmer dude Nov 25 '21 at 08:13
  • 1
    On another note, C doesn't specify that all letters needs to be contiguously encoded. Your loop is not portable. – Some programmer dude Nov 25 '21 at 08:14
  • 1
    You only need one loop, not nested: for each index in the string, read its character, *check it's in range* of the count array, and increment the count. Then report those characters with a non-0 count. It needs a bigger array though: `int cnt[128]` – Weather Vane Nov 25 '21 at 08:15
  • There is an O(n) time and O(n) space solution if you're interested – justANewb stands with Ukraine Nov 25 '21 at 08:15
  • And lastly, after the inner loop is finished, `i` will be outside of the data initialized in the `cnt` array, and `cnt[i]` will be an *indeterminate* value. Assuming ASCII (since your code already uses it) use an array of `26` elements for the counter, and use `ch - 'a'` as an index into it. – Some programmer dude Nov 25 '21 at 08:16
  • @Klas-Kenny Isn't it though? I thought that cnt[100] = {} sets every elemento to 0. When i run the program the output is "a is 0 times", "b is 0 times" etc so I think it is set to 0. – Greggs Nov 25 '21 at 08:18
  • Aside: doing `int x = strlen(A)-1;` and then `i <= x;` is dangerous practice generally when `x` is unsigned and length is `0`. More idiomatic would be `int x = strlen(A); and then i < x;` – Weather Vane Nov 25 '21 at 08:18
  • Some compilers tolerate an empty initialiser `{ }` but it should be `{ 0 }`. – Weather Vane Nov 25 '21 at 08:22
  • @WeatherVane I am not sure how to do it using one loop. I deleted the first for-loop, initialized ch = 'A' before the loop, and checked if A[i] == ch. After that part I increment ch by 1 but still no result. – Greggs Nov 25 '21 at 08:27
  • Like this: `int cnt[128] = {0}; for(size_t i = 0; i < strlen(A); i++) { char ch = A[i]; if(ch >= 0 && ch < 128) cnt[ch]++; }` – Weather Vane Nov 25 '21 at 08:31
  • 1
    @WeatherVane I see, that makes more sense than what I tried. Thanks a lot for your time, yours and everyone's. – Greggs Nov 25 '21 at 08:42
  • Of course, for a large range of values in other circumstances, the `cnt[]` array could be impractically large, so a tree might be better. – Weather Vane Nov 25 '21 at 08:44
  • Trees are something I am yet to learn/discover. I will keep it in mind for when I will have to deal with larger arrays. – Greggs Nov 25 '21 at 08:53

1 Answers1

0

Here you go! A similar problem as yours, feel free to ask any questions.

#include <stdio.h>

int count(char *p, char t) // Function to count occurence //
{
    int i=0;
    
    while(p[0]!= '\0')
    {
        if(p[0]==t)
        {
            i++;
        }
        
        p++;
    }
    
    return i;
}

int main()
{
    char s[1000],c;
    printf("Enter the string: ");
    scanf("%s",s);
    
    
    printf("Enter the character: ");
    scanf("  %c",&c);
    
    int j = count(s,c); // Function call //
    
    printf("The occurence of %c in string is %d",c,j);

    return 0;
}