请编写函数fun,它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。 例如,若输入字符串: 123412132,输入字符为: 1,则输出:3。
时间: 2023-08-31 17:24:48 浏览: 76
### 回答1:
好的,我理解了您的要求。下面是函数fun的代码实现:
```c
int fun(char *ss, char c)
{
int count = 0;
while(*ss != '\0')
{
if(*ss == c)
{
count++;
}
ss++;
}
return count;
}
```
函数fun接收两个参数,第一个参数为指向字符串的指针,第二个参数为需要统计的字符。函数通过遍历字符串,判断每一个字符是否与指定字符相等,若相等则计数器加1,最后返回计数器的值即可。例如,对于输入字符串"123412132"和指定字符'1',函数返回值为3,即字符串中有3个字符是'1'。
### 回答2:
下面是一个实现函数fun的示例代码:
```python
def fun(ss, ch):
count = 0
for char in ss:
if char == ch:
count += 1
return count
```
该函数接受两个参数,ss为需要统计的字符串,ch为需要统计的字符。函数通过遍历字符串中的每个字符,判断是否与指定字符相等,若相等则计数器count加1。最后返回计数器count的值。
你可以调用该函数进行测试,例如:
```python
s = "123412132"
c = "1"
result = fun(s, c)
print("指定字符的个数为:", result)
```
运行结果为:
```
指定字符的个数为: 3
```
### 回答3:
函数fun的编写如下:
```python
def fun(ss, char):
count = 0
for c in ss:
if c == char:
count += 1
return count
```
在这个函数中,我们使用一个变量count来统计指定字符的个数。通过遍历字符串ss中的每个字符,如果当前字符与指定字符相同,则将count加一。最后返回count的值。
例如,若调用fun("123412132", "1"),则输出结果为3,即输入字符串中指定字符"1"的个数为3。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)