简体   繁体   English

如何计算每个单词的字母

[英]How to count letter of each word

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! 谢谢!

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 

Take a look at http://www.tutorialspoint.com/java/java_string_split.htm . 请查看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 " ". 您应该能够使用Java String.split()函数以空格“”分隔字符串。 That should give you an array that contains each word. 这应该给你一个包含每个单词的数组。 Then it is simply finding the length of each word. 然后它只是找到每个单词的长度。

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.");
        }

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());
        }
    }

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. 希望你也可以包括那个。

          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);

this is my answer thanks to Sahil Nagpal for the inspiration: 这是我的回答,感谢Sahil Nagpal的灵感:

package exercise;

import java.util.Scanner; import 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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM