输入字符串,统计字符的个数(循环、函数strlen(arr)两种方法)
时间: 2024-01-02 20:03:03 浏览: 88
好的,下面是两种方法的示例代码:
方法一:循环遍历字符串,统计字符个数
```c
#include <stdio.h>
int main() {
char str[100];
int count = 0, i;
printf("请输入一个字符串:");
gets(str);
for (i = 0; str[i] != '\0'; i++) {
count++;
}
printf("字符个数为:%d\n", count);
return 0;
}
```
方法二:调用函数strlen(arr)统计字符个数
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count;
printf("请输入一个字符串:");
gets(str);
count = strlen(str);
printf("字符个数为:%d\n", count);
return 0;
}
```
需要注意的是,方法一中我们使用了循环遍历字符串,直到遍历到字符串结尾的空字符\0,来统计字符个数;而方法二中我们直接调用了函数strlen(arr)来计算字符串长度。两种方法都能够正确地统计字符个数,具体使用哪种方法取决于实际情况和个人习惯。
相关问题
函数指针写100个字符串中$的个数
好的,我会尽力回答你的问题。对于你的问题,我可以给你一个函数指针的示例代码:
```c
#include<stdio.h>
#include<string.h>
#define MAX_LENGTH 100
int count_char(char *str, char ch)
{
int count = 0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == ch) {
count++;
}
}
return count;
}
void count_dollars(char **arr, int len, int (*fun_ptr)(char*, char))
{
for (int i = 0; i < len; i++) {
int count = (*fun_ptr)(arr[i], '$');
printf("The number of '$' in string %d is: %d\n", i+1, count);
}
}
int main()
{
char *str_arr[MAX_LENGTH] = {"$foo$barz$", "$baz$$quux$$$$$$", "$$$", "$$$quuxfoo$$$"};
count_dollars(str_arr, 4, &count_char);
return 0;
}
```
这个程序定义了一个`count_char`函数,用于计算字符串中某个字符出现的次数。然后定义了一个`count_dollars`函数,用于统计字符串数组中每个字符串中"$"的个数,具体实现是通过函数指针`fun_ptr`来调用`count_char`函数。在`main`函数中,定义了一个字符串数组`str_arr`,并调用`count_dollars`函数来统计"$"的个数。
如果您有更多问题,欢迎问我。
查找一个字符串中所含一个数组的个数
可以使用循环和字符串匹配的方法来查找一个字符串中所含一个数组的个数。具体实现可以参考以下代码:
```c
#include <stdio.h>
#include <string.h>
int count(char *str, char *arr[], int n) {
int cnt = 0;
for (int i = 0; i < n; i++) {
char *p = str;
while ((p = strstr(p, arr[i])) != NULL) {
cnt++;
p += strlen(arr[i]);
}
}
return cnt;
}
int main() {
char str[] = "hello world, this is a test string";
char *arr[] = {"hello", "world", "test"};
int n = sizeof(arr) / sizeof(arr[0]);
int cnt = count(str, arr, n);
printf("The count is %d\n", cnt);
return 0;
}
```
这段代码中,count 函数接受一个字符串 str,一个字符串数组 arr,和数组的长度 n,返回 str 中包含 arr 中所有字符串的个数。函数中使用了两层循环,外层循环遍历数组 arr,内层循环使用 strstr 函数查找字符串 str 中是否包含数组中的字符串,如果找到了就将计数器 cnt 加一,并将指针 p 移动到下一个位置继续查找。最后返回计数器的值即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)