📌  相关文章
📜  计算给定字符串中的单词

📅  最后修改于: 2022-05-13 01:55:34.613000             🧑  作者: Mango

计算给定字符串中的单词

给定一个字符串,计算其中的单词数。单词由以下字符分隔:空格('')或换行符('\n')或制表符('\t')或这些字符的组合。

这个问题可以有很多解决方案。以下是一个简单而有趣的解决方案。
这个想法是保持两种状态:IN 和 OUT。状态 OUT 表示看到了分隔符。状态 IN 表示看到一个单词字符。当前一个状态为 OUT 且下一个字符是单词字符时,我们增加字数。

C++
/* C++ program to count no of words
from given input string. */
#include 
using namespace std;
 
#define OUT 0
#define IN 1
 
// returns number of words in str
unsigned countWords(char *str)
{
    int state = OUT;
    unsigned wc = 0; // word count
 
    // Scan all characters one by one
    while (*str)
    {
        // If next character is a separator, set the
        // state as OUT
        if (*str == ' ' || *str == '\n' || *str == '\t')
            state = OUT;
 
        // If next character is not a word separator and
        // state is OUT, then set the state as IN and
        // increment word count
        else if (state == OUT)
        {
            state = IN;
            ++wc;
        }
 
        // Move to next character
        ++str;
    }
 
    return wc;
}
 
// Driver code
int main(void)
{
    char str[] = "One two     three\n four\tfive ";
    cout<<"No of words : "<


C
/* C program to count no of words from given input string. */
#include 
 
#define OUT    0
#define IN    1
 
// returns number of words in str
unsigned countWords(char *str)
{
    int state = OUT;
    unsigned wc = 0;  // word count
 
    // Scan all characters one by one
    while (*str)
    {
        // If next character is a separator, set the
        // state as OUT
        if (*str == ' ' || *str == '\n' || *str == '\t')
            state = OUT;
 
        // If next character is not a word separator and
        // state is OUT, then set the state as IN and
        // increment word count
        else if (state == OUT)
        {
            state = IN;
            ++wc;
        }
 
        // Move to next character
        ++str;
    }
 
    return wc;
}
 
// Driver program to tes above functions
int main(void)
{
    char str[] = "One two         three\n    four\tfive  ";
    printf("No of words : %u", countWords(str));
    return 0;
}


Java
/* Java program to count no of words
from given input string. */
public class GFG {
  
    static final int OUT = 0;
    static final int IN = 1;
      
    // returns number of words in str
    static int countWords(String str)
    {
        int state = OUT;
        int wc = 0;  // word count
        int i = 0;
         
        // Scan all characters one by one
        while (i < str.length())
        {
            // If next character is a separator, set the
            // state as OUT
            if (str.charAt(i) == ' ' || str.charAt(i) == '\n'
                    || str.charAt(i) == '\t')
                state = OUT;
                 
      
            // If next character is not a word separator
            // and state is OUT, then set the state as IN
            // and increment word count
            else if (state == OUT)
            {
                state = IN;
                ++wc;
            }
      
            // Move to next character
            ++i;
        }
        return wc;
    }
      
    // Driver program to test above functions
    public static void main(String args[])
    {
        String str = "One two       three\n four\tfive  ";
        System.out.println("No of words : " + countWords(str));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Python3 program to count words
# in a given string
OUT = 0
IN = 1
 
# Returns number of words in string
def countWords(string):
    state = OUT
    wc = 0
 
    # Scan all characters one by one
    for i in range(len(string)):
 
        # If next character is a separator,
        # set the state as OUT
        if (string[i] == ' ' or string[i] == '\n' or
            string[i] == '\t'):
            state = OUT
 
        # If next character is not a word
        # separator and state is OUT, then
        # set the state as IN and increment
        # word count
        elif state == OUT:
            state = IN
            wc += 1
 
    # Return the number of words
    return wc
 
# Driver Code
string = "One two         three\n four\tfive "
print("No. of words : " + str(countWords(string)))
 
# This code is contributed by BHAVYA JAIN


C#
// C# program to count no of words
// from given input string.
using System;
 
class GFG {
     
    static int OUT = 0;
    static int IN = 1;
     
    // returns number of words in str
    static int countWords(String str)
    {
        int state = OUT;
        int wc = 0; // word count
        int i = 0;
         
        // Scan all characters one
        // by one
        while (i < str.Length)
        {
            // If next character is a separator,
            // set the state as OUT
            if (str[i] == ' ' || str[i] == '\n'||
                                  str[i] == '\t')
                state = OUT;
                 
     
            // If next character is not a word
            // separator and state is OUT, then
            // set the state as IN and increment
            // word count
            else if (state == OUT)
            {
                state = IN;
                ++wc;
            }
     
            // Move to next character
            ++i;
        }
         
        return wc;
    }
     
    // Driver program to test above functions
    public static void Main()
    {
        String str = "One two     three\n four\tfive ";
        Console.WriteLine("No of words : "
                              + countWords(str));
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


Java
// Java program to count total
// number of words in the string
class GFG
{
     
    // Function to count total number
    // of words in the string
    public static int
      countWords(String str)
    {
         
        // Check if the string is null
        // or empty then return zero
        if (str == null || str.isEmpty())
            return 0;
         
        // Splitting the string around
        // matches of the given regular
        // expression
        String[] words = str.split("\\s+");
         
        // Return number of words
        // in the given string
        return words.length;
    }
     
    // Driver Code
    public static void main(String args[])
    {
         
        // Given String str
        String str =
          "One two       three\n four\tfive ";
         
        // Print the result
        System.out.println("No of words : " +
           countWords(str));
    }
}
// This code is contributed by Prashant Srivastava


Java
// Java program to count total
// number of words in the string
import java.util.StringTokenizer;
class GFG
{
     
    // Function to count total number
    // of words in the string
    public static int
      countWords(String str)
    {
         
        // Check if the string is null
        // or empty then return zero
        if (str    == null || str.isEmpty())
            return 0;
         
        // Create a StringTokenizer with the
        // given string passed as a parameter
        StringTokenizer tokens = new
          StringTokenizer(str);
         
        // Return the number of words
        // in the given string using
        // countTokens() method
        return tokens.countTokens();
    }
     
    // Driver Code
    public static void main(String args[])
    {
         
        // Given String str
        String str =
          "One two       three\n four\tfive ";
         
        // Print the result
        System.out.println("No of words: " +
          countWords(str));
    }
}
// This code is contributed by Prashant Srivastava


Java
// Java program to count total
// number of words in the string
class GFG
{
     
    // Function to count total number
    // of words in the string
    public static int
      countWords(String str)
    {
         
        // Check if the string is null
        // or empty then return zero
        if(str    == null || str.isEmpty())
            return 0;
         
        int wordCount = 0;
         
        boolean isWord = false;
        int endOfLine = str.length() - 1;
         
        // Converting the given string
        // into a character array
        char[] ch = str.toCharArray();
         
        for (int i = 0; i < ch.length; i++) {
             
            // Check if the character is a letter
            // and index of character array doesn't
            // equal to end of line that means, it is
            // a word and set isWord by true
            if (Character.isLetter(ch[i])
                && i != endOfLine)
               
                isWord = true;
             
            // Check if the character is not a letter
            // that means there is a space, then we
            // increment the wordCount by one and set
            // the isWord by false
            else if (!Character.isLetter(ch[i])
                     && isWord) {
               
                wordCount++;
                isWord = false;
            }
             
            // Check for the last word of the
            // sentence and increment the wordCount
            // by one
            else if (Character.isLetter(ch[i])
                     && i == endOfLine)
                wordCount++;
        }
         
        // Return the total number of
        // words in the string
        return wordCount;
         
    }
     
    // Driver Code
    public static void main(String args[])
    {
         
        // Given String str
        String str =
          "One two       three\n four\tfive ";
         
        // Print the result
        System.out.println("No of words : " +
          countWords(str));
    }
}
// This code is contributed by Prashant Srivastava


Python3
# Python program to count total
# number of words in the string
 
# Function to count total number
# of words in the string
def countWords(Str):
     
    # Check if the string is null
    # or empty then return zero
    if(Str == None or len(Str) == 0):
        return 0
     
    wordCount = 0
    isWord = False
    endOfLine = len(Str) - 1
     
    # Converting the given string
    # into a character array
    ch = list(Str)
    for i in range(len(ch)):
         
        # Check if the character is a letter
        # and index of character array doesn't
        # equal to end of line that means, it is
        # a word and set isWord by true
        if(ch[i].isalpha() and i != endOfLine):
            isWord = True
         
        # Check if the character is not a letter
        # that means there is a space, then we
        # increment the wordCount by one and set
        # the isWord by false
        elif(not ch[i].isalpha() and isWord):
            wordCount += 1
            isWord = False
         
        # Check for the last word of the
        # sentence and increment the wordCount
        # by one
        elif(ch[i].isalpha() and i == endOfLine):
            wordCount += 1
     
    # Return the total number of
    # words in the string
    return wordCount
 
# Driver Code
 
# Given String str
Str =  "One two       three\n four\tfive "
 
# Print the result
print("No of words :", countWords(Str))
 
# This code is contributed by rag2127


C#
// C# program to count total
// number of words in the string
using System;
public class GFG
{
 
  // Function to count total number
  // of words in the string
  static int countWords(String str)
  {
 
    // Check if the string is null
    // or empty then return zero
    if(str == null)
    {
      return 0;
    }
    int wordCount = 0;
    bool isWord = false;
    int endOfLine = str.Length - 1;
 
    // Converting the given string 
    // into a character array
    char[] ch = str.ToCharArray();
    for (int i = 0; i < ch.Length; i++)
    {
 
      // Check if the character is a letter
      // and index of character array doesn't
      // equal to end of line that means, it is
      // a word and set isWord by true
      if (Char.IsLetter(ch[i]) 
          && i != endOfLine)
      {
        isWord = true;
      }
 
      // Check if the character is not a letter
      // that means there is a space, then we
      // increment the wordCount by one and set
      // the isWord by false
      else if (!Char.IsLetter(ch[i]) 
               && isWord)
 
      {
        wordCount++;
        isWord = false;
      }
 
      // Check for the last word of the 
      // sentence and increment the wordCount 
      // by one
      else if (Char.IsLetter(ch[i])
               && i == endOfLine)
      {
        wordCount++;
      }
    }
 
    // Return the total number of 
    // words in the string
    return wordCount;
  }
 
  // Driver Code
  static public void Main ()
  {
 
    // Given String str
    string str = "One two       three\n four\tfive ";
 
    // Print the result
    Console.WriteLine("No of words : " + countWords(str));
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出
No of words : 5

方法二:使用String.split()方法

  1. 获取字符串以计算总字数。
  2. 检查字符串是否为空或 null 然后返回 0。
  3. 使用 String 类的 split() 方法在空格处分割字符串。
  4. split() 方法在给定正则表达式的匹配项周围打破给定字符串,并返回一个字符串数组。
  5. 数组的长度是给定字符串中的单词数。
  6. 现在,打印结果。

下面是上述方法的实现:

Java

// Java program to count total
// number of words in the string
class GFG
{
     
    // Function to count total number
    // of words in the string
    public static int
      countWords(String str)
    {
         
        // Check if the string is null
        // or empty then return zero
        if (str == null || str.isEmpty())
            return 0;
         
        // Splitting the string around
        // matches of the given regular
        // expression
        String[] words = str.split("\\s+");
         
        // Return number of words
        // in the given string
        return words.length;
    }
     
    // Driver Code
    public static void main(String args[])
    {
         
        // Given String str
        String str =
          "One two       three\n four\tfive ";
         
        // Print the result
        System.out.println("No of words : " +
           countWords(str));
    }
}
// This code is contributed by Prashant Srivastava


输出
No of words : 5

时间复杂度: O(N)

方法三:使用StringTokenizer.countTokens()方法

  1. 获取字符串以计算总字数。
  2. 检查字符串是否为空或 null 然后返回 0。
  3. 使用作为参数传递的给定字符串创建一个 StringTokenizer。
  4. 使用 countTokens() 方法计算给定字符串中的单词总数。
  5. 现在,打印结果。

下面是上述方法的实现:

Java

// Java program to count total
// number of words in the string
import java.util.StringTokenizer;
class GFG
{
     
    // Function to count total number
    // of words in the string
    public static int
      countWords(String str)
    {
         
        // Check if the string is null
        // or empty then return zero
        if (str    == null || str.isEmpty())
            return 0;
         
        // Create a StringTokenizer with the
        // given string passed as a parameter
        StringTokenizer tokens = new
          StringTokenizer(str);
         
        // Return the number of words
        // in the given string using
        // countTokens() method
        return tokens.countTokens();
    }
     
    // Driver Code
    public static void main(String args[])
    {
         
        // Given String str
        String str =
          "One two       three\n four\tfive ";
         
        // Print the result
        System.out.println("No of words: " +
          countWords(str));
    }
}
// This code is contributed by Prashant Srivastava


输出
No of words: 5

时间复杂度: O(N)

方法四:使用字符.isLetter() 方法

  1. 获取字符串以计算总字数。
  2. 检查字符串是否为空或 null 然后返回 0。
  3. 将给定的字符串转换为字符数组。
  4. 检查字符是否为字母,并且字符数组的索引不等于行尾,这意味着它是一个单词,并将isWord设置为 true。
  5. 检查字符是否不是表示有空格的字母,然后我们将wordCount加一并将isWord设置为 false。
  6. 检查句子的最后一个单词并将wordCount加一。
  7. 现在,打印结果。

下面是上述方法的实现:

Java

// Java program to count total
// number of words in the string
class GFG
{
     
    // Function to count total number
    // of words in the string
    public static int
      countWords(String str)
    {
         
        // Check if the string is null
        // or empty then return zero
        if(str    == null || str.isEmpty())
            return 0;
         
        int wordCount = 0;
         
        boolean isWord = false;
        int endOfLine = str.length() - 1;
         
        // Converting the given string
        // into a character array
        char[] ch = str.toCharArray();
         
        for (int i = 0; i < ch.length; i++) {
             
            // Check if the character is a letter
            // and index of character array doesn't
            // equal to end of line that means, it is
            // a word and set isWord by true
            if (Character.isLetter(ch[i])
                && i != endOfLine)
               
                isWord = true;
             
            // Check if the character is not a letter
            // that means there is a space, then we
            // increment the wordCount by one and set
            // the isWord by false
            else if (!Character.isLetter(ch[i])
                     && isWord) {
               
                wordCount++;
                isWord = false;
            }
             
            // Check for the last word of the
            // sentence and increment the wordCount
            // by one
            else if (Character.isLetter(ch[i])
                     && i == endOfLine)
                wordCount++;
        }
         
        // Return the total number of
        // words in the string
        return wordCount;
         
    }
     
    // Driver Code
    public static void main(String args[])
    {
         
        // Given String str
        String str =
          "One two       three\n four\tfive ";
         
        // Print the result
        System.out.println("No of words : " +
          countWords(str));
    }
}
// This code is contributed by Prashant Srivastava

Python3

# Python program to count total
# number of words in the string
 
# Function to count total number
# of words in the string
def countWords(Str):
     
    # Check if the string is null
    # or empty then return zero
    if(Str == None or len(Str) == 0):
        return 0
     
    wordCount = 0
    isWord = False
    endOfLine = len(Str) - 1
     
    # Converting the given string
    # into a character array
    ch = list(Str)
    for i in range(len(ch)):
         
        # Check if the character is a letter
        # and index of character array doesn't
        # equal to end of line that means, it is
        # a word and set isWord by true
        if(ch[i].isalpha() and i != endOfLine):
            isWord = True
         
        # Check if the character is not a letter
        # that means there is a space, then we
        # increment the wordCount by one and set
        # the isWord by false
        elif(not ch[i].isalpha() and isWord):
            wordCount += 1
            isWord = False
         
        # Check for the last word of the
        # sentence and increment the wordCount
        # by one
        elif(ch[i].isalpha() and i == endOfLine):
            wordCount += 1
     
    # Return the total number of
    # words in the string
    return wordCount
 
# Driver Code
 
# Given String str
Str =  "One two       three\n four\tfive "
 
# Print the result
print("No of words :", countWords(Str))
 
# This code is contributed by rag2127

C#

// C# program to count total
// number of words in the string
using System;
public class GFG
{
 
  // Function to count total number
  // of words in the string
  static int countWords(String str)
  {
 
    // Check if the string is null
    // or empty then return zero
    if(str == null)
    {
      return 0;
    }
    int wordCount = 0;
    bool isWord = false;
    int endOfLine = str.Length - 1;
 
    // Converting the given string 
    // into a character array
    char[] ch = str.ToCharArray();
    for (int i = 0; i < ch.Length; i++)
    {
 
      // Check if the character is a letter
      // and index of character array doesn't
      // equal to end of line that means, it is
      // a word and set isWord by true
      if (Char.IsLetter(ch[i]) 
          && i != endOfLine)
      {
        isWord = true;
      }
 
      // Check if the character is not a letter
      // that means there is a space, then we
      // increment the wordCount by one and set
      // the isWord by false
      else if (!Char.IsLetter(ch[i]) 
               && isWord)
 
      {
        wordCount++;
        isWord = false;
      }
 
      // Check for the last word of the 
      // sentence and increment the wordCount 
      // by one
      else if (Char.IsLetter(ch[i])
               && i == endOfLine)
      {
        wordCount++;
      }
    }
 
    // Return the total number of 
    // words in the string
    return wordCount;
  }
 
  // Driver Code
  static public void Main ()
  {
 
    // Given String str
    string str = "One two       three\n four\tfive ";
 
    // Print the result
    Console.WriteLine("No of words : " + countWords(str));
  }
}
 
// This code is contributed by avanitrachhadiya2155

Javascript



输出
No of words : 5

时间复杂度: O(N)