-1

I want to write down some method that receive a string and return me the most common letter in small version.(only letters) For example - "aabbbAA" will return a. "766&%- aabbB" will return b.

I tried to write it down but i don't know how to recognize between Capital letters and small letters. And how to recognize between digits and letters.

JAVA

Thank ;)

NM2
  • 127
  • 6

1 Answers1

0
    int[] counters = new int['z' - 'a' + 1];
    for( int i = 0; i < counters.length; i++ ) {
        counters[i] = 0;
    }

    String str = new String("absgsAAAAs");
    for( int i = 0; i < str.length(); i++ ) {
        if( str.charAt( i ) >= 'a' && str.charAt( i ) <= 'z' ) {
            counters[str.charAt( i ) - 'a']++;
        } else if( str.charAt( i ) >= 'A' && str.charAt( i ) <= 'Z' ) {
            counters[str.charAt( i ) - 'A']++;
        }
    }

    int maxi = 0;
    for( int i = 1; i < counters.length; i++ ) {
        if( counters[i] > counters[maxi] ) {
            maxi = i;
        }
    }
    System.out.println( Character.toChars( 'a' + maxi ) );
Anton Todua
  • 667
  • 4
  • 15
  • I have to count the Capital and the small letters Together. For example ''aaAAbbb'' will return me 'a'. – NM2 Nov 22 '15 at 20:30