Created
October 15, 2018 04:35
-
-
Save noynaert/392e39435b0e7aa98e46d14f65e8f83d to your computer and use it in GitHub Desktop.
Example of counting special characters in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
int count = getSymbolCount("Hello!#@#!!!!!!!"); | |
System.out.println("Count is "+count); | |
} | |
public static int getSymbolCount(String word){ | |
final String SYMBOLS = "~!@#$%"; //This is not the full list of characters that need to be checked! | |
int result = 0; | |
for(int i = 0; i< SYMBOLS.length();i++) { | |
char ch = SYMBOLS.charAt(i); | |
System.out.printf("Symbol %d is %c\n", i, ch); | |
if(word.indexOf(ch) > -1) | |
result++; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment