1

So here is my code and I am struggling with the ispunct(); it starts counting in 8 and in isalpha() it starts counting in 1 so I print it noAlpha-1, but in ispunct() it is awkward to put noSpecial-8 to become accurate. I don't have any issues in digits. What might be the problem here?

#include<stdio.h>
#include<ctype.h>

int main(){
    
    char string[100];
    int i, noAlpha, noDigit, noSpecial;
        
       printf("Input the string : ");
       gets(string);    

 
        noAlpha=0;
        noDigit=0;
        noSpecial=0;
        
        for(i=0;i<100;i++) {
     
        if(isalpha(string[i]))
        noAlpha++;
    
        
        if(isdigit(string[i]))
        noDigit++;
        
        if(ispunct(string[i]))
        noSpecial++;
    }
        
        printf("Number of Alphabets in the string is %d\n", noAlpha-1);
        printf("Number of Digits in the string is %d\n", noDigit);
        printf("Number of Special characters in the string is %d\n", noSpecial);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
singkit
  • 11
  • 1
  • 2
    First of all, ***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 even have been removed from the C language. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Apr 01 '22 at 11:43
  • Please [edit] and show a **simple** example of input and expected vs. actual output. Also explain the `-1` in `noAlpha-1`. Also indent your code properly. It's hard to work with poorly indented code, even for top programmers. – Jabberwocky Apr 01 '22 at 11:44
  • Secondly, don't forget that strings are really called ***null-terminated** strings*. What happens if the user inputs a string with only five characters? What do you think happen when you loop over the null-terminator and continue into the other 94 characters in the array that weren't initialized? – Some programmer dude Apr 01 '22 at 11:45
  • I saw fgets() when I try to search this. Unfortunately in my school we used gets() on this. I will practice fgets(). Thanks – singkit Apr 01 '22 at 11:46
  • Don't forget return from main as well. – Gnqz Apr 01 '22 at 11:47
  • I am really sorry, this is my first time using stackoverflow. So sorry – singkit Apr 01 '22 at 11:47
  • 3
    You are *probably* counting characters that you didn't actually input. Rather than `i<100`, use `i < strlen(string)`. – Adrian Mole Apr 01 '22 at 11:47
  • @Gnqz No explicit `return 0;` is needed since the C99 standard. If there's no explicit `return` statement before the closing `}` the compiler will implicitly add it. Note that this is a special case for the `main` function. – Some programmer dude Apr 01 '22 at 11:54
  • 2
    Don't use `gets` or `fgets` or `scanf` for this. This is the right place to use `getchar` – William Pursell Apr 01 '22 at 11:54
  • 3
    @AdrianMole — or, better, use `string[i] != '\0'` to test when to exit the loop. – Jonathan Leffler Apr 01 '22 at 12:00

2 Answers2

4

The function gets is unsafe and is not supported by the C Standard.

Either use fgets like

fgets( string, sizeof( string ), stdin );

or scanf like

scanf( "%99[^\n]", string );

The entered string can be less than the size of the array string. So this loop

for(i=0;i<100;i++) 

invokes undefined behavior.

Instead you should use

for(i=0; string[i] != '\0'; i++) 

It is better to rewrite the if statements like if-else statements

    unsigned char c = string[i];

    if( isalpha( c ) )
    {
        noAlpha++;
    }
    else if( isdigit( c ) )
    {
        noDigit++;
    }
    else if( ispunct( c ) )
    {
        noSpecial++;
    }

So the output will look like

    printf("Number of Alphabets in the string is %d\n", noAlpha);
    printf("Number of Digits in the string is %d\n", noDigit);
    printf("Number of Special characters in the string is %d\n", noSpecial);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1
#include<stdio.h>
#include<ctype.h>

int main(){
    
    char string[100];
    int i, noAlpha, noDigit, noSpecial;
        
       printf("Input the string : ");
       scanf( "%99[^\n]", string );    

 
        noAlpha=0;
        noDigit=0;
        noSpecial=0;
        
        for(i=0; string[i] != '\0'; i++) {
     
        unsigned char c = string[i];

        if( isalpha( c ) )
        {
        noAlpha++;
        }
        else if( isdigit( c ) )
        {
        noDigit++;
        }
        else if( ispunct( c ) )
        {
        noSpecial++;
        }
        }
        
        printf("Number of Alphabets in the string is %d\n", noAlpha-1);
        printf("Number of Digits in the string is %d\n", noDigit);
        printf("Number of Special characters in the string is %d\n",noSpecial);
}
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
ASMITH
  • 29
  • 5