Write a function, in C or Python, that returns the number of lowercase letters in a chain of characters passed as a parameter. For example, if the chain is Semi-annual, the function must return the number 9.
解答如下
#include <stdio.h>
#include <string.h>
int Count_lowercase_letters(char str[])
{
int count=0,i;
for(i=0;i<strlen(str);i++)
{
if(str[i]<='z'&&str[i]>='a')
{
count++;
}
}
return count;
}
int main()
{
char str[150];
gets(str);
printf("The number of lowercase letters:%d\n",Count_lowercase_letters(str));
return 0;
}
s='Semi-annual'
n=0
for i in s:
if 'a'<=i<='z':
n+=1
print(n)
用python的话可以使用正则替换,然后统计剩余字符数
import re
s = 'Semi-annual'
print(len(re.sub('[^a-z]+','',s)))
没理解错的话,这道题是要编写一个函数实现对输入字符串中的小写字母进行统计把,C代码如下:
#include<stdio.h>
#include<string.h>
int fun(char s[],int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if (s[i] >= 'a' && s[i] <= 'z')
count++;
return count;
}
int main()
{
char s[100] = {'\0'};
int len, result;
gets(s); //输入字符串
len = strlen(s); //计算字符串长度
result = fun(s,len); //调用函数,将结果传给result
printf("%d\n", result);//输出字符串中小写字母个数
}