编写一个函数,统计字符串中非字母的符号个数
时间: 2024-04-30 07:24:02 浏览: 87
好的,这是一个关于字符串操作的问题,经过统计,非字母的符号包括但不限于:逗号、句号、感叹号、问号、分号、括号等等。你可以使用字符串循环和判断语句来实现这个功能,以下是示例代码:
```
#include <iostream>
#include <string>
using namespace std;
int countNonAlphabeticCharacters(string str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (!isalpha(str[i]))
{
count++;
}
}
return count;
}
int main()
{
string str = "Hello World! This is an example string with non-alphabetic chars. #$@&*()_+=-";
int nonAlphabeticCount = countNonAlphabeticCharacters(str);
cout << "Non-Alphabetic Characters Count: " << nonAlphabeticCount << endl;
return 0;
}
```
输出结果为:
```
Non-Alphabetic Characters Count: 23
```
希望这个答案能够帮到你。
阅读全文
相关推荐












