Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Thursday 18 January 2018

Number of occurences of characters in a string

import java.util.Scanner;
class RepeatCha
{
public static void main(String arg[])
{
System.out.println("enter string :");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char[] c=str.toCharArray();
int loopcount=0;
int count=0;

for(int i=0;i<c.length;i++)
{
//System.out.println(c[i]+" "+i);
boolean flag=true;
for(int k=0;k<i;k++){
if(c[i]==(str.charAt(k)))
flag=false;
}
if(flag){
for(int j=0;j<str.length();j++)
{
if(c[i]==str.charAt(j))
count=count+1;
}
System.out.println(c[i]+" : "+" "+(count));
count=0;
loopcount++;
}
}
}
}
--------------------------------------------------------------------------------
Output :  
enter string :
sarfraz
s :  1
a :  2
r :  2
f :  1
z :  1

No comments:

Post a Comment