Question
program to print the least occurring character in a given sentence.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class leastOccurringCharacterInSentence
{
public static void main(String[] args)
{
String sen="";
char ch=' ',ch1=' ',leastOccuring=' ';
int i=0,len=0,count=0,min=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
sen = sc.nextLine();
sen=sen.toLowerCase();
len=sen.length();
min=len;
for(ch='a';ch<='z';ch++)
{
count=0;
for(i=0;i< len;i++)
{
ch1=sen.charAt(i);
if(ch1==ch)
{
count++;
}
}
if(count< min&& count>0)
{
min=count;
leastOccuring=ch;
}
}
System.out.println("least occurring character in given sentence:"+leastOccuring);
}
}