×

Java Programs

Java Practice

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

This program will read a string and print used different characters/letters in java.

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

Core Java Example Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.