设计一个函数int countlet(char*p),能够统计出p指向的字符串里英文字母的个数,在主函数中输入一个字符串并调用该函数输出统计结果 c➕➕
时间: 2024-05-05 19:18:25 浏览: 25
好的,我可以回答这个问题。以下是代码实现:
```
#include <iostream>
using namespace std;
int countlet(char* p) {
int cnt = 0;
while(*p) {
if((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) {
cnt++;
}
p++;
}
return cnt;
}
int main() {
char s[100];
cout << "请输入一个字符串:";
cin.getline(s, 100);
int res = countlet(s);
cout << "字符串中英文字母的个数为:" << res << endl;
return 0;
}
```
在此,我已经完成了你的问题回答。如果你有更多的问题,可以继续问我。
相关问题
编写程序用于统计一个字符串中字母、数字、空格以及其它字符的个数。在主函数中输入字符串后,调用函数:void fun(char *p, int n),并输出统计结果。要求用指针实现。
下面是一个简单的实现:
```c++
#include <iostream>
using namespace std;
void fun(char *p, int n)
{
int letters = 0, digits = 0, spaces = 0, others = 0;
for(int i = 0; i < n; i++)
{
if(isalpha(*(p+i))) // 判断字母
letters++;
else if(isdigit(*(p+i))) // 判断数字
digits++;
else if(*(p+i) == ' ') // 判断空格
spaces++;
else // 其它字符
others++;
}
cout << "字母个数:" << letters << endl;
cout << "数字个数:" << digits << endl;
cout << "空格个数:" << spaces << endl;
cout << "其它字符个数:" << others << endl;
}
int main()
{
char str[100];
cout << "请输入一个字符串:" << endl;
cin.getline(str, 100);
fun(str, strlen(str));
return 0;
}
```
在这个程序中,我们定义了一个`fun`函数用于统计字母、数字、空格以及其它字符的个数。在主函数中,我们先输入一个字符串,然后调用`fun`函数进行统计,并输出结果。`fun`函数中使用了`isalpha`和`isdigit`函数来判断字母和数字。
编写一个函数,int cap_count(char* s):返回输入字符串 s 中大写字母的个数。
好的,以下是满足条件的 C 语言函数代码:
```c
int cap_count(char* s) {
int count = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
count++;
}
}
return count;
}
```
该函数通过循环遍历输入字符串中的每个字符,判断该字符是否为大写字母,如果是,则计数器加一。最后返回计数器的值,即大写字母的个数。