Dec

18

/*Write a program in Java to accept a sentence and convert all its letters into upper case. Count and print the number of letters of the English alphabet that have been used in the sentence (i.e. if a letter occurs 1 or more times it is counted as 1).
For example, if the sentence entered is:
“FOR ME PARENTS ARE GOOD ON EARTH”
Then the output should be: 13 letters.
*/

— Thank you Tarushi, CMS Gomti Nagar, Lucknow for asked this questions

import java.util.*;
public class Prog2
{
public void main(String str){
str=str.toUpperCase(); //to Convert sentence stored in str into Capital Letters
int len=str.length(); // to get the length of the sentence
int count=0; // count variable will be used to count the unique chars in sentence.
String temp=””; // temp string we are using to hold strings have only alphabets step by step in below loop
for(int i=0;i<len;i++)
{
char ch=str.charAt(i); //storing single char of each step in ch variable
if(ch<=’A’ && ch<=’Z’) // condition to check whether ch has an alphabet
{
if(temp.indexOf(ch)==-1) //condition to check; did char in ch exists in temp if not increase count.
count++;
temp=temp+ch; // put every char in temp fetch in each iteration
}
}
System.out.println(str); // to print sentence in capital letters
System.out.println(count+” letters”); // to print unique occurence of letters in accepted sentence.
}
}


Leave a Reply