How to count the special characters in a string in C

1 Answer

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

int main(void)
{
    int special_characters = 0;
    char *p, *s = "@C Programming!!! Rules~$^^^";
    
    p = s;
    while (*p != '\0') 
    {
        if (!isdigit(*p) && !isalpha(*p))
            special_characters++;
            
        p++;
    }
    printf("special characters = %i", special_characters);
     
    return 0;
}
 
 

/*
run:
 
special characters = 11

*/


answered Oct 1, 2014 by avibootz
...