ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.