ADVERTISEMENT
ADVERTISEMENT

C program to count digits, spaces, special characters, alphabets in a string

In this C program, we are going to learn how to count digits, spaces, special characters and alphabets?.

Given a string and we have to count digits, spaces, special characters and alphabets using C program.

/*C program to count digits, spaces, special characters, 
alphabets in a string.*/

#include <stdio.h>

int main()
{
    char str[100];
    int countDigits, countAlphabet, countSpecialChar, countSpaces;
    int counter;

    //assign all counters to zero
    countDigits = countAlphabet = countSpecialChar = countSpaces = 0;

    printf("Enter a string: ");
    gets(str);

    for (counter = 0; str[counter] != NULL; counter++) {

        if (str[counter] >= '0' && str[counter] <= '9')
            countDigits++;
        else if ((str[counter] >= 'A' && str[counter] <= 'Z') || (str[counter] >= 'a' && str[counter] <= 'z'))
            countAlphabet++;
        else if (str[counter] == ' ')
            countSpaces++;
        else
            countSpecialChar++;
    }

    printf("\nDigits: %d \nAlphabets: %d \nSpaces: %d \nSpecial Characters: %d", countDigits, countAlphabet, countSpaces, countSpecialChar);

    return 0;
}

Output

Enter a string: This is test string, 123 @#% 98.

Digits: 5
Alphabets: 16
Spaces: 6
Special Characters: 5

C Strings User-defined Functions Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.