在主函数中从键盘读取一个字串符,并调用自定义函数int fun计算其中包含的非英文字母字符数量
时间: 2024-05-06 15:19:06 浏览: 11
好的,我来给你实现一个简单的代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int fun(char *str){
int count = 0;
int len = strlen(str);
for(int i=0; i<len; i++){
if((str[i]<'a' || str[i]>'z') && (str[i]<'A' || str[i]>'Z')){
count++;
}
}
return count;
}
int main(){
char str[100];
cin >> str;
int count = fun(str);
cout << "非英文字母字符数量为:" << count << endl;
return 0;
}
```
在这个程序中,我们首先定义了一个函数fun用于计算字符串中的非英文字母字符数量。接着在主函数中,我们从键盘读取一个字符串,调用fun函数计算其中的非英文字母字符数量,并输出结果。
在fun函数中,我们使用了一个for循环遍历字符串的每个字符,如果该字符不是英文字母,则计数器加1。最后返回计数器的值。
相关问题
在主函数中从键盘读取一个字串符,并调用自定义函数int fun(char a[])计算其中包含的非英文字母字符数量
好的,下面是示例代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int fun(char a[]) {
int count = 0;
for (int i = 0; i < strlen(a); i++) { // 遍历字符串
if (!isalpha(a[i])) { // 如果当前字符不是英文字母
count++; // 非英文字母计数器加1
}
}
return count;
}
int main() {
char str[100];
cout << "请输入一个字符串:";
cin >> str;
int count = fun(str);
cout << "非英文字母字符数量为:" << count << endl;
return 0;
}
```
该程序中,自定义函数`fun`的参数为一个字符数组,返回值为非英文字母的数量。在主函数中,通过`cin`从键盘读取一个字符串,并将字符串作为参数传递给`fun`函数计算非英文字母数量,最终输出结果。
在主函数中从键盘读取一个字串符,并调用自定义函数int fun(char a[])计算其中包含的非英文字母字符数量,并在主函数中输出结果
下面是示例代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int fun(char a[]) {
int count = 0;
for (int i = 0; i < strlen(a); i++) {
if (!isalpha(a[i])) { // 如果a[i]不是英文字母
count++; // 非英文字母计数器加1
}
}
return count;
}
int main() {
char str[100];
cout << "请输入一个字符串:";
cin >> str;
int count = fun(str);
cout << "非英文字母数量为:" << count << endl;
return 0;
}
```
输入一个字符串后,调用`fun`函数计算其中包含的非英文字母数量,并输出结果。`fun`函数遍历字符串中的每个字符,如果某个字符不是英文字母,则将计数器加1。最后返回计数器的值。