Q:

Java program to print used different characters (letters) in a string

0

Java program to print used different characters (letters) in a string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Print Different Characters/Letters in a String using Java program

//Java program to print used different characters (letters) in a string.
 
import java.util.Scanner;
 
public class PrintDiffChar {
    
   public static void main(String[] args) {
    
      String text;
      int count;
      char ch;
       
      Scanner SC=new Scanner(System.in);
             
      System.out.println("Write something here...: ");
      text = SC.nextLine();
       
      //converting string into upper case
      text = text.toUpperCase();
       
      count = 0;
      System.out.println("Following characters are used in the input text:");
      for ( ch = 'A'; ch <= 'Z'; ch++ ) {
          int i;  //character index in string
          for ( i = 0; i < text.length(); i++ ) {
              if ( ch == text.charAt(i) ) {
                  System.out.print(ch + " ");
                  count++;
                  break;
              }
          }
      }
       
      System.out.println("\nTotal number of different characters are: " + count);
    
   }
    
}

Output

    
    Write something here...: 
    Hello friends, this is me.
    Following characters are used in the input text:
    D E F H I L M N O R S T 
    Total number of different characters are: 12

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Core Java Example programs for Beginners and Professionals

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to print table of an integer number... >>