Skip to content

Instantly share code, notes, and snippets.

@jimmy087
Created May 4, 2013 06:16
Show Gist options
  • Save jimmy087/5516447 to your computer and use it in GitHub Desktop.
Save jimmy087/5516447 to your computer and use it in GitHub Desktop.
A program for finding out the upper case letter in a string.
// A program for finding out the upper case letter in a string.
public class Exercise9_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter a string: ");
String s1 = input.nextLine();
int [] counts1 = countUpperCaseLetters(s1);
for (int i = 0; i < counts1.length - 1; i++) {
if (counts1[i] != 0) {
System.out.println((char)('A' + i) + " appears " + counts1 [i] + ((counts1 [i] == 1) ? " time." : " times."));
}
}
}
// A method for counting upper case letters in a string.
private static int[] countUpperCaseLetters(String s1) {
int [] counts1 = new int[26];
for (int i = 0; i < s1.length() - 1; i++) {
if (Character.isUpperCase(s1.charAt(i))) {
counts1 [s1.charAt(i) - 'A']++;
}
}
return counts1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment