📌  相关文章
📜  检查给定的字符串是否是有效的英文单词

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

检查给定的字符串是否是有效的英文单词

给定字符串str ,任务是检查该字符串str 是否包含有效的英文单词。

如果字符串满足以下所有条件,则该字符串被称为有效的英语单词 -

  • 字符串只能有一个大写字符作为第一个字符。
  • 字符串只能包含小写字符。
  • 该字符串只能由一个连字符('-')组成,两端被字符包围。
  • 字符串不能包含任何数字。
  • 如果有任何标点符号,它必须只有一个,并且必须出现在末尾。

打印字符串str 中有效单词的数量。

方法:

  • 初始化变量ans以保持对有效字数的计数。
  • 循环遍历句子中出现的每个单词。
  • 检查单词的每个字母,看它是否符合问题陈述中提到的标准。
  • 如果不满足任何条件,则返回 false。
  • 如果单词满足所有条件,则增加变量ans 的值。
  • 打印变量ans 的值。

下面是上述方法的C++程序——

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find valid words
bool ValidWords(string sentence)
{
    int hyphen = 0;
    int size = sentence.size();
    if (isupper(sentence[0])) {
        for (int i = 0; i < size; i++) {
            // Check for numbers
            if (isdigit(sentence[i]))
                return false;
            if (isupper(sentence[i]))
                return false;
 
            if (isalpha(sentence[i]))
                continue;
 
            if (sentence[i] == '-') {
                // Only 1 hyphen is allowed
                if (++hyphen > 1)
                    return false;
 
                // hyphen should be surrounded
                // by letters
                if (i - 1 < 0
                    || !isalpha(sentence[i - 1])
                    || i + 1 >= size
                    || !isalpha(sentence[i + 1]))
                    return false;
            }
 
            // Punctuation must be at the
            // end of the word
            else if (i != size - 1
                     && ispunct(sentence[i]))
                return false;
        }
    }
    else
        return true;
}
 
// Driver code
int main()
{
    string sentence = "i Love- Geeks-Forgeeks!";
 
    istringstream s(sentence);
    string word;
    int ans = 0;
 
    while (s >> word)
        if (ValidWords(word))
            ans++;
 
    // Display the result
    cout << ans << " words";
}


Java
/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
 
    // Function to find valid words
    static boolean ValidWords(String sentence)
    {
        int hyphen = 0;
        int size = sentence.length();
        if (Character.isUpperCase(sentence.charAt(0))) {
            for (int i = 0; i < size; i++) {
                // Check for numbers
                if (Character.isDigit(sentence.charAt(i)))
                    return false;
                if (Character.isUpperCase(
                        sentence.charAt(i)))
                    return false;
 
                if (Character.isAlphabetic(
                        sentence.charAt(i)))
                    continue;
 
                if (sentence.charAt(i) == '-') {
                    // Only 1 hyphen is allowed
                   hyphen = hyphen +1 ;
                    if (hyphen > 1)
                        return false;
 
                    // hyphen should be surrounded
                    // by letters
                    if (i - 1 < 0
                        || !Character.isAlphabetic(
                            sentence.charAt(i - 1))
                        || i + 1 >= size
                        || !Character.isAlphabetic(
                            sentence.charAt(i + 1)))
                        return false;
                }
 
                // Punctuation must be at the
                // end of the word
                else if (i != size - 1
                         && ((sentence.charAt(i) == '!'
                              || sentence.charAt(i) == ','
                              || sentence.charAt(i) == ';'
                              || sentence.charAt(i) == '.'
                              || sentence.charAt(i) == '?'
                              || sentence.charAt(i) == '-'
                              || sentence.charAt(i) == '\''
                              || sentence.charAt(i) == '\"'
                              || sentence.charAt(i)
                                     == ':')))
                    return false;
            }
        }
        else
            return true;
      return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String sentence = "i Love- Geeks-Forgeeks!";
        int ans = 0;
        String words[] = sentence.split(" ");
        for (String word : words) {
            if (ValidWords(word)==true){
                ans++;
            }
        }
 
        // Display the result
        System.out.print(ans + " words");
    }
}


C#
/*package whatever //do not write package name here */
using System;
 
class GFG
{
 
  // Function to find valid words
  static bool ValidWords(String sentence)
  {
    int hyphen = 0;
    int size = sentence.Length;
    if (char.IsUpper(sentence[0]))
    {
      for (int i = 0; i < size; i++)
      {
         
        // Check for numbers
        if (char.IsDigit(sentence[i]))
          return false;
        if (char.IsUpper(sentence[i]))
          return false;
 
        if (char.IsLetter(sentence[i]))
          continue;
 
        if (sentence[i] == '-')
        {
           
          // Only 1 hyphen is allowed
          hyphen = hyphen + 1;
          if (hyphen > 1)
            return false;
 
          // hyphen should be surrounded
          // by letters
          if (i - 1 < 0
              || !char.IsLetter(sentence[i - 1])
              || i + 1 >= size
              || !char.IsLetter(sentence[i + 1]))
            return false;
        }
 
        // Punctuation must be at the
        // end of the word
        else if (i != size - 1
                 && ((sentence[i] == '!'
                      || sentence[i] == ','
                      || sentence[i] == ';'
                      || sentence[i] == '.'
                      || sentence[i] == '?'
                      || sentence[i] == '-'
                      || sentence[i] == '\''
                      || sentence[i] == '\"'
                      || sentence[i]
                      == ':')))
          return false;
      }
    }
    else
      return true;
    return false;
  }
 
  // Driver code
  public static void Main()
  {
    String sentence = "i Love- Geeks-Forgeeks!";
    int ans = 0;
    String[] words = sentence.Split(" ");
    foreach (String word in words)
    {
      if (ValidWords(word) == true)
      {
        ans++;
      }
    }
 
    // Display the result
    Console.Write(ans + " words");
  }
}
 
// This code is contributed by gfgking.


Javascript


输出
1 words


时间复杂度: O(N)
辅助空间: O(N)