-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfLetter.java
More file actions
25 lines (25 loc) · 866 Bytes
/
Copy pathNumberOfLetter.java
File metadata and controls
25 lines (25 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Nancy
/* Have the user input a String (word, phrase, sentence, paragraph, etc)
* and indicate a letter of interest. Use a for loop to determine and print
* out how many times that letter appears in the String.
*/
import java.util.*;
public class NumberOfLetter
{
public static void main (String [] args){
Scanner scan = new Scanner(System.in);
System.out.println("Please input a string");
String str = scan.nextLine();
System.out.println("Please indicate a letter of interest");
String letter = scan.nextLine();
int length = str.length();
int number = 0;
for (int i = 0; i < length; i++){
if (str.substring(i, i+1).equals(letter)){
number ++;
}
}
System.out.println(letter + " appears " + number + " times in " +
str);
}
}