【问题标题】:Most common letter ( Capital and small) in a string字符串中最常见的字母(大写和小写)
【发布时间】:2015-11-23 04:34:34
【问题描述】:

我想写一些接收字符串的方法,并以小版本返回最常见的字母。(只有字母) 例如 - “aabbbAA”将返回一个。 "766&%- aabbB" 将返回 b。

我试着把它写下来,但我不知道如何区分大写字母和小写字母。 以及如何识别数字和字母。

JAVA

谢谢 ;)

【问题讨论】:

  • 你使用什么程序语言?
  • Java,抱歉。

标签: java string


【解决方案1】:
    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 ) );

【讨论】:

  • 我必须同时数大写和小写字母。例如 ''aaAAbbb'' 将返回给我 'a'。
猜你喜欢
  • 2021-01-06
  • 2019-08-08
  • 2020-01-04
  • 1970-01-01
  • 2013-04-14
  • 2011-06-16
  • 2014-10-03
  • 1970-01-01
  • 2011-01-16
相关资源
最近更新 更多