Skip to content

Instantly share code, notes, and snippets.

@noynaert
Created October 15, 2018 04:35
Show Gist options
  • Save noynaert/392e39435b0e7aa98e46d14f65e8f83d to your computer and use it in GitHub Desktop.
Save noynaert/392e39435b0e7aa98e46d14f65e8f83d to your computer and use it in GitHub Desktop.
Example of counting special characters in a string
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