java 如何计算每个单词的字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27725274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:17:04  来源:igfitidea点击:

How to count letter of each word

javaandroidstringintword

提问by amirhossein Nezamlou

I was wondering how I would write a method to count the number of words and number of each word letters for example if input is "The Blue Sky" in return i take something that show me there was 3 words 3 letter 4 letter 3 letter

我想知道如何编写一种方法来计算单词的数量和每个单词字母的数量,例如如果输入是“蓝天”作为回报,我会拿一些东西告诉我有 3 个单词 3 个字母 4 个字母 3 个字母

i'v found this code already

我已经找到了这个代码

public static int countWords(String s){

    int wordCount = 0;

    boolean word = false;
    int endOfLine = s.length() - 1;

    for (int i = 0; i < s.length(); i++) {
        // if the char is a letter, word = true.
        if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
            word = true;
            // if char isn't a letter and there have been letters before,
            // counter goes up.
        } else if (!Character.isLetter(s.charAt(i)) && word) {
            wordCount++;
            word = false;
            // last word of String; if it doesn't end with a non letter, it
            // wouldn't count without this.
        } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
            wordCount++;
        }
    }
    return wordCount;
}

I really appreciate any help I can get! Thanks!

我真的很感激我能得到的任何帮助!谢谢!

回答by Prem

Step 1 - Find the number of words in the sentence using the space separator.

步骤 1 - 使用空格分隔符查找句子中的单词数。

 String CurrentString = "How Are You";
    String[] separated = CurrentString.split(" ");
    String sResultString="";
    int iWordCount = separated.length;
    sResultString = iWordCount +" words";

Step 2 - Find letter count in each word.

第 2 步 - 找出每个单词中的字母数。

    for(int i=0;i<separated.length;i++)
    {
    String s = separated[i];
    sResultString = sResultString + s.length + " letters ";
    }

// Print sResultString 

回答by Jroosterman

Take a look at http://www.tutorialspoint.com/java/java_string_split.htm. You should be able to use the Java String.split() function to break up the string by spaces " ". That should give you an array that contains each word. Then it is simply finding the length of each word.

看看http://www.tutorialspoint.com/java/java_string_split.htm。您应该能够使用 Java String.split() 函数按空格“”拆分字符串。那应该给你一个包含每个单词的数组。然后它只是找到每个单词的长度。

回答by Junaid

to count words this might help

计算单词这可能会有所帮助

  public static int countWords(String str)
        {
            int count = 1;
            for (int i=0;i<=str.length()-1;i++)
            {
                if (str.charAt(i) == ' ' && str.charAt(i+1)!=' ')
                {
                    count++;
                }
            }
            return count;
        }
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter a sentence: ");
            String sentence = in.nextline();
            System.out.print("Your sentence has " + countWords(sentence) + " words.");
        }

回答by mukul28.03

Here is my code-

这是我的代码-

   public void countWordsLetters(String s){
        String str[]=s.split(" ");
        System.out.println("No. of words in string::"+str.length);
        for (int i=0;i<str.length;i++){
            System.out.println("No of letters in "+i+" word "+str[i].length());
        }
    }

回答by Sahil Nagpal

Buddy, most of the above answers are correct but nobody considered the whitespace while counting the characters in String. Hope you can also include that.

伙计,上面的大多数答案都是正确的,但没有人在计算 String 中的字符时考虑空格。希望你也可以包含它。

          for(int j=0;j<name.length();j++){ //here is name is my string

          if(Character.isWhitespace(name.charAt(j))){

            }else{
                count+=1;
            }

      }
      System.out.println("The word count is "+count);

回答by ahmed

this is my answer thanks to Sahil Nagpal for the inspiration:

这是我的回答,感谢 Sahil Nagpal 的启发:

package exercise;

import java.util.Scanner;

导入 java.util.Scanner;

public class method_letter {

公共类method_letter {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("enter any string");
    String s=in.nextLine();
    System.out.println("the letter count :"+lettercount(s));

}
public static int lettercount (String s) {
    int count=0;
    for (int i=0; i<s.length(); i++) {
        if(Character.isWhitespace(s.charAt(i))){

        }else {
            count+=1;
        }
    }
    return count;
}

}

}