首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >(Java)计算句子中的字母?

(Java)计算句子中的字母?
EN

Stack Overflow用户
提问于 2018-07-31 11:26:11
回答 5查看 4.2K关注 0票数 1

以下是我的代码:

代码语言:javascript
复制
char[] array = new char[26] ;

    int index = 0 ;
    int letter = 0 ;
    int countA = 0 ;

    String sentence = "Once upon a time..." ;

    if(sentence.contains("."))
    {
        String sentenceNoSpace = sentence.replace(" ", "").toLowerCase() ;
        String sentenceFinal = sentenceNoSpace.substring(0, sentenceNoSpace.indexOf(".")) ;
        char[] count = new char[sentenceFinal.length()] ;
        for (char c = 'a'; c <= 'z'; c++) 
        {
            array[index++] = c ;
            for(int i = 0; i < sentenceFinal.length(); i++)
            {
                if(sentenceFinal.charAt(i) == c)
                    count[letter++] = c ; 
                //if(sentenceFinal.charAt(i) == 'a')    
                    //countA++ ;   
            }

        }
        String result = new String(count) ; // Convert to a string.
        System.out.println("\n" + result) ;

        System.out.println("\nTotal number of letters is " + result.length()) ;
        System.out.println(countA) ;
    }
    else
    {
       System.out.println("You forgot a period. Try again.") ;
    }

我在计算给定句子中有多少个a,b,c等时遇到了困难。有一种方法可以做到这一点,就是这个部分

代码语言:javascript
复制
//if(sentenceFinal.charAt(i) == 'a')    
                //countA++ ;

我可以一直创建到z,有没有更有效的方法?

注意:不要使用Hashmap或任何其他高级技术。

EN

回答 5

Stack Overflow用户

发布于 2018-07-31 11:41:12

使用将存储每个字母计数的int数组letterCounts。假设可以忽略字母的大小写,则letterCounts数组的长度将为26。

迭代字符串的字符并更新数组中相应的整数。使用其ASCII值查找相应的索引,如下所示。

letterCounts[c - 97]++

97是'a‘的ASCII值,其计数需要存储在索引0处。

这样,从字符的ASCII值中减去97将得到该字符的相应索引。

注意:这是假设您想要存储小写字母的计数。

票数 1
EN

Stack Overflow用户

发布于 2018-07-31 11:50:45

在不使用映射的情况下非常麻烦,但这将计算字符串中的所有字符。

您可能想要修改以排除诸如空格等内容。

代码语言:javascript
复制
public class Main {

    public static void main(String[] args) {
        String sentence = "Once upon a time...";

        // Create an array of size 256 ASCII_SIZE
        int count[] = new int[256];
        int length = sentence.length();

        // Initialize count array index
        for (int i = 0; i < length; i++)
            count[sentence.charAt(i)]++;

        // Create an array of given String size
        char chars[] = new char[sentence.length()];
        for (int i = 0; i < length; i++) {
            chars[i] = sentence.charAt(i);
            int find = 0;
            for (int j = 0; j <= i; j++) {

                // If any matches found
                if (sentence.charAt(i) == chars[j])
                    find++;
            }

            if (find == 1) {
               System.out.println("Occurrence of " + sentence.charAt(i) + " is:" + count[sentence.charAt(i)]);

            }
        }
    }
}

以下哪项输出:

代码语言:javascript
复制
Occurrence of O is:1
Occurrence of n is:2
Occurrence of c is:1
Occurrence of e is:2
Occurrence of   is:3
Occurrence of u is:1
Occurrence of p is:1
Occurrence of o is:1
Occurrence of a is:1
Occurrence of t is:1
Occurrence of i is:1
Occurrence of m is:1
Occurrence of . is:3
票数 0
EN

Stack Overflow用户

发布于 2018-07-31 11:59:24

检查下面的代码,你可以有一个26长度的数组,索引将根据字母表的存在而递增。

代码语言:javascript
复制
public void getResult(){

        int [] charCount = new int [26];
        int countA = 0 ;

        String sentence = "Once upon a time..." ;

        if(sentence.contains("."))
        {
            String sentenceNoSpace = sentence.replace(" ", "").toLowerCase() ;
            String sentenceFinal = sentenceNoSpace.substring(0, sentenceNoSpace.indexOf(".")) ;

            char[] sentenceCharArray = sentenceFinal.toCharArray();
            //char a = 97;
            for (int i = 0; i <sentenceCharArray.length ; i++) {
                int index = sentenceCharArray[i] - 97 ;
                if(index >= 0 && index <= 26) {
                    charCount[index] += 1;
                }
            }


            System.out.print("Result : ");

            for (int i = 0; i < charCount.length ; i++) {
                System.out.print(charCount [i]+" , ");
            }


            System.out.println("\nTotal number of letters is " + sentenceCharArray.length) ;
        }
        else
        {
            System.out.println("You forgot a period. Try again.") ;
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51605038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档