Created
October 12, 2018 01:57
-
-
Save noynaert/92bf346fd61b7684c94e85996902b530 to your computer and use it in GitHub Desktop.
Quickie demo of counting specific characters from another 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) { | |
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