簡體   English   中英

如何計算每個單詞的字母

[英]How to count letter of each word

我想知道如何編寫一個方法來計算單詞的數量和每個單詞字母的數量,例如,如果輸入是“藍天”,作為回報,我拿了一些東西,告訴我有3個字3個字母4個字母3個字母

我已經找到了這個代碼

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

我真的很感激我能得到的任何幫助! 謝謝!

第1步 - 使用空格分隔符查找句子中的單詞數。

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

第2步 - 在每個單詞中查找字母數。

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

// Print sResultString 

請查看http://www.tutorialspoint.com/java/java_string_split.htm 您應該能夠使用Java String.split()函數以空格“”分隔字符串。 這應該給你一個包含每個單詞的數組。 然后它只是找到每個單詞的長度。

算一下這可能會有所幫助

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

這是我的代碼 -

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

好友,上面的大部分答案都是正確的,但是在計算字符串中的字符時沒有人考慮空格。 希望你也可以包括那個。

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

這是我的回答,感謝Sahil Nagpal的靈感:

package exercise;

import java.util.Scanner;

公共類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