Count the special character in an sentences



Problem to count the special character in an string.



Example-1:


Input    : S = "@my#crazy$coding*"
Output   : 4  
Explain  : Special character are (@,#,$,*).Total number of special character are 4.


Example-2:


Input    : S = "&helloworld%"
Output   : 2  
Explain  : Special character are (&,%).Total number of special character are 2.   







Solution




public class Main
{
    public static void main(String [] args)
    {
        String s = "@my#crazy$coding*";

        int count = 0;

        for(int i = 0; i<s.length(); i++) 
        {

            if(s.charAt(i)>=97&&s.charAt(i)<=122||s.charAt(i)>=65&&s.charAt(i)<=90||s.charAt(i)>=48&&s.charAt(i)<=57)
            {
                continue;
            }

            count++;
        }

        System.out.print(count);
    }
}
n = "@my#crazy$coding*"

k = 0

for i in range(len(n)):

    if(not n[i].isalnum()):

        k += 1

print(k)



Output



4