This Java program takes input from the user in the form of a string and then counts the total number of different characters used in that string.
Here is how the program works:
import java.util.Scanner; public class Different_Char { public static void main(String[] args) { String text; int count; char ch; Scanner input = new Scanner(System.in); System.out.print("Enter the String : "); text = input.nextLine(); text = text.toUpperCase();//converting string into uppercase count = 0; System.out.print("Following characters are used in the input Text :"); for ( ch = 'A'; ch <= 'Z'; ch++ ) { int i; for ( i = 0; i < text.length(); i++ ) //index in string { if ( ch == text.charAt(i) ) { System.out.print(ch + " "); count++; break; } } } System.out.println("\nTotal Number of Different Characters : " + count); } }
Enter the String : Hello World Following characters are used in the input Text :D E H L O R W Total Number of Different Characters : 7
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions