1

在这里编码很新。我们正在使用 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;
}
4

4 回答 4

2

尝试以下操作:

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;
}
于 2020-12-02T04:23:40.947 回答
1

感谢大家的帮助,以下代码似乎运行良好。我将变量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;
}
于 2020-12-02T04:30:41.353 回答
1

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

希望能帮助到你?

于 2020-12-02T04:34:00.117 回答
1

你有两种问题:

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

逻辑是:

(1) 是 alpha 字符串 <=>所有字符都是 alpha

对立

(2) is not alpha string <=> 它至少存在一个非 alpha char

因此代码类似于:

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));
}
于 2020-12-02T04:23:10.037 回答