0
int main (){
    char string[100];
    int c=0,count[26]={0},x;
    gets(string);
    while(string[c]!='\0'){
        if (string[c] >= 'a' && string[c] <= 'z') {
            x = string[c] - 'a';
            count[x]++;
        }
        c++;
    }

    for (c = 0; c < 26; c++){
        printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
    }
    return 0;
}

Firstly, it gives me this error about get: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration] gets(string); And I don't know why doesn't work, although it makes sense.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
nadia
  • 13
  • 2
  • 1
    It's a warning, not an error. – Tony Tannous May 29 '18 at 05:14
  • 4
    [Don't use `gets()` ever — it isn't safe!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) And do `#include ` when you use any I/O functions declared in it. – Jonathan Leffler May 29 '18 at 05:14
  • 1
    works for me (both with using the `gets` and if I ignore the `gets` and use a predefined string). Can you show your I/O? – CIsForCookies May 29 '18 at 05:17
  • @TonyTannous Would you be interested in an efficient way to create consistent indentation in StackOverflow editor? – Yunnosch May 29 '18 at 05:18
  • @Yunnosch I would – CIsForCookies May 29 '18 at 05:18
  • @Yunnosch I am on my phone. No preview to how it looks. I wanted to make it clear `count[x]` is in the if block. – Tony Tannous May 29 '18 at 05:19
  • You could test it with any of of these sentences: (1) `the quick brown fox jumps over the lazy dog.` (2) `pack my box with five dozen liquor jugs.` (3) `the five boxing wizards jump quickly.` (4) `how vexingly quick daft zebras jump.` (5) `bright vixens jump; dozy fowl quack.` You could add code to print each character as you process it, and identify whether it is (lower-case) alphabetic or not. – Jonathan Leffler May 29 '18 at 05:20
  • @TonyTannous C uses the friendly "nice program you've got here, it'd be a shame something happens to it" kind of warning. – n. m. could be an AI May 29 '18 at 05:20
  • @CIsForCookies this is what I get: a occurs 0 times in the string. b occurs 0 times in the string. and so on for all other letters – nadia May 29 '18 at 05:21
  • @nadia So please show your Input/Output – CIsForCookies May 29 '18 at 05:22
  • gets is not safe to use because it does not check the array bound.Link:https://www.geeksforgeeks.org/fgets-gets-c-language/ – Thakur Karthik May 29 '18 at 05:24
  • Do you have an RTF file or something horrific like that? Are you running on Windows or a Unix-like system? (You're using a modern GCC, it would seem, that uses C11 by default — so not as old as GCC 4.x unless you're tweaking the compiler options.) The code works for me, albeit with a warning about it using `gets()` when it is run. – Jonathan Leffler May 29 '18 at 05:25
  • @TonyTannous My respect for working from the phone. But you should take more time with the restricted editor. Even without a preview, the method I describe here should also work: https://chat.stackoverflow.com/rooms/171957/room-for-yunnosch-and-cisforcookies – Yunnosch May 29 '18 at 05:27
  • @CIsForCookies My input is aab, and the output is a occurs 0 times in the string. b occurs 0 times in the string. c occurs 0 times in the string. d occurs 0 times in the string. ans so forth – nadia May 29 '18 at 05:30
  • 1
    I tested your code and it works fine. The problem should be elsewhere, maybe the way you input the text. Just try something like `echo aab | your_prog`. – Joël Hecht May 29 '18 at 05:31
  • @nadia from Jonathan Leffler, Joël Hecht checks, the code works fine, so the problem lies elsewhere (not IO, not code). Maybe describe (as Jonathan Leffler suggests) your machine and compiler? [and recheck that your code is the same as the one you posted...] – CIsForCookies May 29 '18 at 05:35

3 Answers3

2

Implicit declaration means that the function must be defined before calling or its prototype must be defined and this code is generating implicit declaration warning because gets() have been removed from c11 standard due to its incompetency while checking array bound and it's prototype is not present in stdio header file. Otherwise the program is working fine.

2

gets() is risky to use.It suffers from Buffer Overflow as gets() doesn’t do any array bound testing. gets() keeps on reading until it sees a newline character.

Try scanf("%99[^\n]", string); instead of your code gets(string); .

Try this modified code.This will work:-

#include <stdio.h>
int main (){
    char string[100];
    int c=0,count[26]={0},x;
    scanf("%99[^\n]", string);
    while(string[c]!='\0'){
        if (string[c] >= 'a' && string[c] <= 'z') {
            x = string[c] - 'a';
            count[x]++;
        }
        c++;
    }

    for (c = 0; c < 26; c++){
        printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
    }
    return 0;
}

Another solution is using fgets().

You can use fgets(string,100, stdin); instead of gets(string); .

Modified code will be :-

#include <stdio.h>
int main (){
    char string[100];
    int c=0,count[26]={0},x;
    fgets(string,100, stdin);  // 100 is the Max Limit of array
    while(string[c]!='\0'){
        if (string[c] >= 'a' && string[c] <= 'z') {
            x = string[c] - 'a';
            count[x]++;
        }
        c++;
    }

    for (c = 0; c < 26; c++){
        printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
    }
    return 0;
}
anoopknr
  • 3,177
  • 2
  • 23
  • 33
  • 1
    This can still produce garbage output. You're not checking the return value of `scanf` to see whether it was successful. – melpomene May 29 '18 at 05:41
0
#include <stdio.h>

int main (){
    char string[100];
    int c=0,count[26]={0},x;
    scanf("%s",&string);
    while(string[c]!='\0'){
        if (string[c] >= 'a' && string[c] <= 'z') {
            x = string[c] - 'a';
            count[x]++;
        }
        c++;
    }

    for (c = 0; c < 26; c++){
        printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
    }
    return 0;
}

enter image description here

Thakur Karthik
  • 3,034
  • 3
  • 15
  • 24
  • 2
    Although the code may be correct, it would be better if you can include explanation (what have you changed, and why) – user202729 May 29 '18 at 05:24
  • 3
    Please don't post screenshots of text. – n. m. could be an AI May 29 '18 at 05:26
  • 3
    `scanf("%s",&string);` is just as unsafe as `gets`, but worse in that it uses `scanf` for user input (`scanf` is token based, not line based, contrary to user expectations) and gets it wrong (`%s` takes a `char *`, not a `char (*)[100]`. – melpomene May 29 '18 at 05:28