Skip to content

Instantly share code, notes, and snippets.

@noynaert
Created October 12, 2018 01:57
Show Gist options
  • Save noynaert/92bf346fd61b7684c94e85996902b530 to your computer and use it in GitHub Desktop.
Save noynaert/92bf346fd61b7684c94e85996902b530 to your computer and use it in GitHub Desktop.
Quickie demo of counting specific characters from another string
public class Main {
public static void main(String[] args) {
String symbols = "!@#$?%^";
print("Happy", symbols);
print("Happy!", symbols);
print("Happy?", symbols);
print("¿Happy?", symbols);
print("?Happy?", symbols);
print("!!!@@@#Happy?", symbols);
print("", symbols);
print(symbols, symbols);
print((symbols + symbols), symbols);
}
public static boolean containsSymbol(String word, char symbol) {
boolean result = (word.indexOf(symbol)>-1);
return result;
}
public static int countSymbols(String word, String symbols){
int result = 0;
for(int i=0;i<symbols.length();i++)
if(containsSymbol(word, symbols.charAt(i)))
result++;
return result;
}
public static void print(String word, String symbols){
System.out.printf("Word: %-15s Symbols: %-15s Count: %d\n", word, symbols, countSymbols(word, symbols));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment