Friday, January 8, 2010

Coding for character frequency in a string java..pls get me?

If I understood your question well: Here you have one of many solutions:





public class FreqString {


public static void main(String[] args){


String s = ';Guadalajara';;


char[] c = s.toCharArray(); // converting to a char array


int freq =0;


for(int i = 0; i%26lt; c.length;i++) {


if(c[i]=='a') // looking for 'a' only


freq++;


}


System.out.println(';Total chars ';+c.length);


System.out.println(';Number of 'a' are ';+freq);


}


}

No comments:

Post a Comment