Question
Using Java language,

Write a program that read a string from the keyboard and counts the number of the letter A and the number of letter B and then prints the result
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution :

CODE :


package countchar;

import java.util.Scanner;

public class CountChar {

  
public static void main(String[] args) {
  
Scanner sc = new Scanner(System.in);
int countA=0, countB=0;
// ask user to enter string here
System.out.println("Enter String to count 'A' and 'B' : ");
String inputString = sc.nextLine();
  
// count number of A and B.
for(int i=0; i<inputString.length(); i++){
if(inputString.charAt(i) == 65){
countA++;
}
else if(inputString.charAt(i) == 66){
countB++;
}
  
}
  
  
// print the output :
System.out.println("Number of 'A' in String : " + countA);
System.out.println("Number of 'B' in String : " + countB);
}
  
}

Sample output :

if you have any doubts then you can ask in comment section if you find the solution helpful then upvote the answer. Thank you.

Add a comment
Know the answer?
Add Answer to:
Using Java language, Write a program that read a string from the keyboard and counts the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT