【问题标题】:How do I check if there is a special character in a string in C++ using isalpha?如何使用 isalpha 检查 C++ 中的字符串中是否有特殊字符?
【发布时间】:2020-12-02 04:01:02
【问题描述】:

对这里的编码很陌生。我们正在使用 isalpha 函数,但我无法将它与字符串一起使用。我的程序将提示用户输入一个单词,然后该函数将检查该单词是否包含任何特殊字符。基本上,如果它们都是特殊字符,我的代码只会说有一个特殊字符,而不是只有几个。我假设这与我的 for 循环有关,但我不知道如何让它工作。我搜索了很多,在 C++ 中找不到太多帮助。

这是我的功能。任何帮助表示赞赏。

//*****IsAlphaStr*****
//This function returns true if the cString passed contains all alphabetic characters.
//If the parameter does not contain all alpha characters, a value of false is returned.
bool IsAlphaStr(char wordcheck[25], bool alphabetic)
{
    int i = 0;
    int n = 0;

    for (int i = 0, n = strlen(wordcheck); i < n; i++)
    {   
        if (isalpha(wordcheck[i]) == 0)
            alphabetic = false;
        else
            alphabetic = true;
    }


    return alphabetic;
}

【问题讨论】:

  • 我搜索了很多,在 C++ 中找不到 mych 帮助。 -- std::all_of。无需编写循环。
  • 提示:想想第二次循环会发生什么。无论如何(真或假),您都将为alphabetic 分配一个值。这意味着您将覆盖循环的第一次迭代中发生的任何事情......这意味着第一个字符对最终结果没有影响。您的代码实际上只说有一个特殊字符,如果它是 last 字符。
  • 提示——为什么你知道字符串中有一个非字母字符时还要继续循环?
  • 提示:std::string 并强烈避免使用固定长度的字符缓冲区,尤其是作为参数。
  • 我们以前从未使用过它,我正在研究的章节是关于数组和 cStrings,我很确定我们应该使用循环来遍历数组中的每个字符。

标签: c++ isalpha


【解决方案1】:

尝试以下方法:

bool IsAllSpecialCharacters(char wordcheck[25], bool alphabetic)
{
    int i = 0;
    int n = strlen(wordcheck)
    for (int i = 0; i < n; i++)
    {   
        if (isalpha(wordcheck[i])) return false
    }
    return true;
}

【讨论】:

    【解决方案2】:

    如前所述,IsAlphaStr 仅在所有给定字符都是字母时才返回true。这可以通过在if 条件的false 分支中添加break 来实现,这会停止for 循环的进一步执行。

            if (isalpha(wordcheck[i]) == 0)
            {
                alphabetic = false;
                break;
            }
    

    整个测试程序是:

    #include <iostream>
    
    //*****IsAlphaStr*****
    //This function returns true if the cString passed contains all alphabetic characters.
    //If the parameter does not contain all alpha characters, a value of false is returned.
    bool IsAlphaStr(char wordcheck[25], bool alphabetic)
    {
        int i = 0;
        int n = 0;
    
        for (int i = 0, n = strlen(wordcheck); i < n; i++)
        {   
            if (isalpha(wordcheck[i]) == 0)
            {
                alphabetic = false;
                break;
            }
            else
                alphabetic = true;
        }
        return alphabetic;
    }
    
    int main()
    {
        char test1[25] = "abcdefghijklmnopqrstuvwx";
        char test2[25] = "0123456789ABCDEFGHIJKLMN";
        char test3[25] = "abcdefghijklmnopqres++-A";
        char test4[25] = "abcdefABCDEF";
        bool alphabetic = false;
    
        alphabetic = IsAlphaStr(test1, alphabetic);
        std::cout << "test1 = " << alphabetic << std::endl;
        alphabetic = IsAlphaStr(test2, alphabetic);
        std::cout << "test2 = " << alphabetic << std::endl;
        alphabetic = IsAlphaStr(test3, alphabetic);
        std::cout << "test3 = " << alphabetic << std::endl;
        alphabetic = IsAlphaStr(test4, alphabetic);
        std::cout << "test4 = " << alphabetic << std::endl;
    
        return 0;
    }
    

    输出是:

    test1 = 1
    test2 = 0
    test3 = 0
    test4 = 1
    

    希望有帮助吗?

    【讨论】:

    • @time_in_real_time:不客气。如果你觉得这很有用,别忘了点赞。
    【解决方案3】:

    感谢大家的帮助,下面的代码似乎运行良好。我将变量Alphabetic 声明为false,然后将其更改为true,然后删除了将其从false 更改回true 的else 语句。这是代码,

    bool IsAlphaStr(char wordcheck[25], bool alphabetic)
    {
        int i = 0;
        int n = 0;
    
        for (int i = 0, n = strlen(wordcheck); i < n; i++)
        {   
            if (isalpha(wordcheck[i]) == 0)
            alphabetic = false;
        }
    
        return alphabetic;
    }
    

    【讨论】:

      【解决方案4】:

      你有两种问题:

      1. 与逻辑相关的一个
      2. 一个 C++ 相关的

      逻辑是:

      (1) 是 alpha 字符串 所有 字符都是 alpha

      对位

      (2) 是 不是 字母字符串 它至少存在一个非字母字符

      因此代码类似于:

      For all char c in string
         if c is not char return false    <--- (2 in action)
      End for
      
      return true <--- (1 in action)
      

      您必须在 C 或 C++ 之间进行选择。请不要像 C 一样使用 C++ 编写代码。

      如果你想学习 C++,https://en.cppreference.com/w/ 是一个很好的信息来源。

      一个可能的C++解决方案如下:

      #include <string>
      #include <iostream>
      
      bool isAlphaStr(const std::string& to_check)
      {
        for(auto c:to_check) 
          if(!std::isalpha(c)) return false;
        
        return true;
      }
      
      int main()
      {
       char string_1[]="Hello world!";
       std::string string_2{"Hello"};
      
        std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_1);
        std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_2);
      }
      

      为了比较 C++ 风格和 C 风格,我添加了一个纯 C 版本:

      #include <string.h>
      #include <ctype.h> // for isalpha
      #include <stdio.h>
      #include <stdbool.h>
       
      bool isAlphaStr(const char *const to_check)
      {
        const size_t n = strlen(to_check);
        for(size_t i=0;i<n;++i) 
          if(!isalpha(to_check[i])) return false;
        
        return true;
      }
      
      int main()
      {
       char string_1[]="Hello world!";
       char string_2[]="Hello";
      
       printf("\nIs alpha? %d", isAlphaStr(string_1));
       printf("\nIs alpha? %d", isAlphaStr(string_2));
      }
      

      关于 Wyck 评论,这里是带有 bool alphabetic 变量的版本:

      C++:

      #include <string>
      #include <iostream>
      #include <type_traits>
      
      bool isAlphaStr(const std::string& to_check, bool alphabetic)
      {
        if(to_check.empty()) return alphabetic;
        
        for(auto c:to_check) 
          if(!std::isalpha(c)) return false;
        
        return true;
      }
      
      int main()
      {
       char string_1[]="Hello world!";
       std::string string_2{"Hello"};
      
       std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_1,false);
       std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_2,false);
      }
      

      C:

      #include <stdio.h>
      #include <stdbool.h>
       
      bool isAlphaStr(const char *const to_check, bool alphabetic)
      {
        const size_t n = strlen(to_check);
      
        if(!n) return alphabetic; // empty string special case
        
        for(size_t i=0;i<n;++i) 
          if(!isalpha(to_check[i])) return false;
        
        return true;
      }
      
      int main()
      {
       char string_1[]="Hello world!";
       char string_2[]="Hello";
      
       printf("\nIs alpha? %d", isAlphaStr(string_1,false));
       printf("\nIs alpha? %d", isAlphaStr(string_2,false));
      }
      

      【讨论】:

      • 我不知道你对 C++ 的编码有什么了解,就像你在 C 中一样。我们还没有使用过 string 类,它直到下学期才会出现,现在看起来像我这样做的方式是我可能知道的唯一方式。
      • 好的,我明白你的意思了,谢谢你的帮助!
      • @MeowBox C++ 是一门复杂的语言,有很多东西要学。我只是想指出,使用 C 风格的 C++ 编码可能会产生不好的副作用(例如原始指针与智能指针,但我想您稍后会看到)。好好学习,祝你好运!
      • @PicaudVincent 在原始代码中,空字符串的返回结果作为 alphabetic 参数传递到函数中,而您已修改行为以返回空字符串的 true .这是根本不同的。你的并不令人惊讶,而且合乎逻辑——但在这方面表现不同。 (我怀疑最初的行为可能是无意的。)
      • @Wyck 你是绝对正确的我错过了原始代码!等一下,我会解决这个问题的!
      猜你喜欢
      • 2015-11-04
      • 2020-03-15
      • 2021-11-21
      • 2010-12-20
      • 2013-11-27
      • 2014-11-25
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多