编写一个函数totsubstrnum(char *str,char *substr),它的功能是统计子字符串substr在字符串str中出现的次数
时间: 2024-02-27 21:53:25 浏览: 21
好的,下面是函数的实现:
```c
int totsubstrnum(char *str, char *substr) {
int count = 0;
int str_len = strlen(str);
int substr_len = strlen(substr);
for (int i = 0; i <= str_len - substr_len; i++) {
if (strncmp(str + i, substr, substr_len) == 0) {
count++;
}
}
return count;
}
```
函数的参数是两个字符串指针 `str` 和 `substr`,返回值是子字符串 `substr` 在字符串 `str` 中出现的次数。
函数的实现比较简单,就是遍历字符串 `str` 的所有可能的子字符串,如果找到了一个与 `substr` 相同的子字符串,就将计数器 `count` 加 1。最后返回计数器的值即可。
注意,这里使用了 C 语言的库函数 `strncmp` 来比较两个字符串的前缀是否相同。`strncmp` 函数的第一个参数是一个指向字符串的指针,第二个参数是另一个指向字符串的指针,第三个参数是要比较的字符数。如果前缀相同,就返回 0,否则返回一个非零值。
希望能够帮助到你!
相关问题
c++编写一个函数 int fun(char *str,char*substr),该函数可以统计子字符串substr在母字符串str中出现的次数,并将该次数作为函数值返回。
好的,以下是C++代码实现:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int fun(char *str, char *substr){
int count = 0;
int str_len = strlen(str);
int substr_len = strlen(substr);
for(int i=0; i<=str_len-substr_len; i++){
bool match = true;
for(int j=0; j<substr_len; j++){
if(str[i+j] != substr[j]){
match = false;
break;
}
}
if(match){
count++;
}
}
return count;
}
int main(){
char str[100], substr[100];
cin >> str >> substr;
int count = fun(str, substr);
cout << count << endl;
return 0;
}
```
函数`fun`接收两个参数:原字符串`str`和子字符串`substr`,返回子字符串在原字符串中出现的次数。函数内部使用两层循环遍历原字符串,依次比较每个子串是否等于目标子字符串,如果相等则计数器加1。最后返回计数器的值即可。
在主函数中,你可以输入原字符串和子字符串,程序会调用`fun`函数计算出子字符串在原字符串中出现的次数,并输出结果。
设计函数char *insert(str,substr,n),用指针实现在字符串str中的指定位置n处插入字符串substr。代码
以下是实现该功能的函数代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *insert(char *str, char *substr, int n) {
int len1 = strlen(str);
int len2 = strlen(substr);
int i, j;
// 创建一个新字符串,用于存储插入后的结果
char *result = (char *)malloc((len1 + len2 + 1) * sizeof(char));
if (!result) {
printf("Memory allocation failed\n");
exit(1);
}
// 将原始字符串的前n个字符复制到新字符串中
for (i = 0; i < n && i < len1; i++) {
result[i] = str[i];
}
// 将要插入的子字符串复制到新字符串中
for (j = 0; j < len2; j++) {
result[i+j] = substr[j];
}
// 将原始字符串剩余的部分复制到新字符串中
for (j = n; j < len1; j++) {
result[i+j] = str[j];
}
// 在新字符串的末尾添加终止符
result[len1 + len2] = '\0';
return result;
}
int main() {
char str[100] = "Hello, world!";
char substr[100] = "beautiful ";
char *result = insert(str, substr, 6);
printf("%s\n", result);
free(result);
return 0;
}
```
在这个函数中,我们首先计算了原始字符串和要插入的子字符串的长度,然后创建了一个新的字符串来存储插入后的结果。接着,我们将原始字符串的前n个字符复制到新字符串中,然后将要插入的子字符串复制到新字符串中,最后将原始字符串剩余的部分复制到新字符串中。在新字符串的末尾添加终止符,然后返回新字符串的指针。
在main函数中,我们使用了这个函数来将一个字符串插入到另一个字符串的指定位置,并打印出结果。然后,我们释放了动态分配的内存,以避免内存泄漏。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)