Java Program to Print Used Different Characters (Letters) In a String

You are currently viewing Java Program to Print Used Different Characters (Letters) In a String

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

//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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply