如何使用 isalpha 检查 C++ 中的字符串中是否有特殊字符?
How do I check if there is a special character in a string in C++ using isalpha?
刚接触编码。我们正在使用 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++相关的
逻辑是:
(1) is alpha string <=> all chars are alpha
对位
(2) is not alpha string <=> it exists at least one non 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));
}
尝试以下操作:
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;
}
感谢大家的帮助,下面的代码似乎运行良好。我将我的变量 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;
}
如前所述,如果所有给定字符都是字母,IsAlphaStr
应仅 return 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
希望对您有所帮助?
刚接触编码。我们正在使用 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++相关的
逻辑是:
(1) is alpha string <=> all chars are alpha
对位
(2) is not alpha string <=> it exists at least one non 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));
}
尝试以下操作:
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;
}
感谢大家的帮助,下面的代码似乎运行良好。我将我的变量 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;
}
如前所述,如果所有给定字符都是字母,IsAlphaStr
应仅 return 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
希望对您有所帮助?