Write a program to perform the following tasks. Define an array of characters of
ID: 3743868 • Letter: W
Question
Write a program to perform the following tasks. Define an array of characters of size 15. Read 15 characters from the user into the array. Next, read a search character from the user. The program should search for this character in the array. If found, report so, and also report the number times the search character occurs in the input array. Finally, the program should report what other character(s) in the input array occur(s) the same number of times as the search character. Suppose that your 15 character input is “aaaabbbbccccdef”, and the search character is ‘a’. Your program would state that ‘a’ is present in the input and that it occurs 4 times. It would also output stating that “The characters ‘b’ and ‘c’ occur the same number of times as that of ‘a’”.
Explanation / Answer
Here we are using character array to store the input from user and searchChar is the character which we need to check if that is present in the input array
We are using Map to store the characters present in the array and corresponding occurence count
we will first Update Map with characters and count and then check if it has SearchChar present
if present will print the count and traverse the Map to see if there are any other characters with same number of occurences if yes we will print them to
Please find below code and sample output for reference
Please feel free to get back if you need any further help
JAVA Code
package learning;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CharArray {
public static void main(String[] args) {
//Character array of size 15 to store input
char[] a = new char[15];
//Scanner object to take input from user
Scanner in = new Scanner (System.in);
//for loop to take input from user
//using in.next().charAt(0) to get character at 0th position as in.next() returns String
for(int i=0;i<15;i++)
{
System.out.println("Enter the "+(i+1)+"th character");
a[i]=in.next().charAt(0);
}
//Search character
char searchChar;
System.out.println("Enter the search character");
searchChar=in.next().charAt(0);
//Map is used here to store characters and their respective number of occurrences in input
Map<Character,Integer> s = new HashMap<Character,Integer>();
//traversing through the input and storing corresponding count in map
for(int i=0;i<15;i++)
{
if(s.containsKey(a[i]))
{
s.put(a[i], s.get(a[i])+1);
}
else
{
s.put(a[i], 1);
}
}
//System.out.println(s);
//If condition to check if the given character is present in the input array
//If present we will iterate through the map to see if there are any other characters present in the array with mentioned count
if(s.containsKey(searchChar))
{
System.out.println("'"+searchChar+"' is present in the input array for "+s.get(searchChar)+" times.");
for (Character key : s.keySet()) {
if(s.get(key) == s.get(searchChar) && key != searchChar)
System.out.println("Character '" + key+"' occurs same number of types as '"+searchChar+"'");
}
}
}
}
Sample Output
Enter the 1th character
a
Enter the 2th character
a
Enter the 3th character
a
Enter the 4th character
a
Enter the 5th character
a
Enter the 6th character
b
Enter the 7th character
b
Enter the 8th character
b
Enter the 9th character
b
Enter the 10th character
b
Enter the 11th character
c
Enter the 12th character
g
Enter the 13th character
f
Enter the 14th character
r
Enter the 15th character
j
Enter the search character
a
'a' is present in the input array for 5 times.
Character 'b' occurs same number of types as 'a'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.