Saturday, 23 July 2016

Q.4 Print the Character Count in Entered String
i/p:APPLE o/p:A-1 P-2 L-1 E-1
import java.util.Scanner;

public class CharCount {

public static void main(String[] args) {
int count=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String s=sc.next();
char ch[]=s.toCharArray();
for(int i=0;i<ch.length;i++){
count=1;
for(int j=i+1;j<ch.length;j++){
if(ch[i]==ch[j]){
count++;
ch[j]='@';
}
}
if(ch[i]!='@'){
System.out.println(ch[i]+" "+count);
}

}
}
}

No comments:

Post a Comment

Featured post

All Logical Questions are Answered JumpInToIt