c语言.编写一个函数,接收一个字符串,统计其中任意两个及两个以上相邻字符相同的子串的个数
时间: 2023-12-28 09:05:34 浏览: 35
以下是一个简单的 C 语言函数实现,用于统计给定字符串中相邻字符相同的子串的个数:
```c
#include <stdio.h>
#include <string.h>
int countSubstrings(char str[]) {
int len = strlen(str);
int count = 0;
for (int i = 0; i < len - 1; i++) {
int j = i + 1;
while (j < len && str[j] == str[i]) {
j++;
}
if (j - i > 1) {
count += j - i - 1;
}
}
return count;
}
int main() {
char str[] = "abccbaa";
int count = countSubstrings(str);
printf("Number of substrings: %d", count);
return 0;
}
```
该函数接受一个字符串作为参数,遍历字符串的每个字符,检查其是否与后面的字符相同。如果找到一组相邻的相同字符,则将其作为一个子串计数器加一。最后返回计数器的值。
例如,对于输入字符串 "abccbaa",该函数将返回 3,因为它包含三个相邻字符相同的子串: "cc","cc" 和 "aa"。
相关问题
c语言编写一个函数,接收一个字符串,统计其中任意两个及两个以上相邻字符相同的子串的个数
以下是一个C语言函数,用于统计一个字符串中任意两个及两个以上相邻字符相同的子串的个数:
```c
#include <string.h>
int countSubstr(char *str)
{
int len = strlen(str);
int count = 0;
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (str[i] == str[j]) {
count++;
for (int k = i + 1, l = j + 1; k < j && l < len; k++, l++) {
if (str[k] == str[l]) {
count++;
} else {
break;
}
}
}
}
}
return count;
}
```
该函数接收一个字符串作为参数,然后使用嵌套的for循环遍历字符串中的所有字符对,统计相邻字符相同的子串的个数。具体地,函数使用i和j两个循环变量遍历字符串中的所有字符对,如果字符对相同,则使用k和l两个循环变量从i+1和j+1的位置开始遍历字符串,统计任意两个及两个以上相邻字符相同的子串的个数。最后,函数返回统计结果。
.编写一个函数,接收一个字符串,统计其中任意两个及两个以上相邻字符相同的子串的个数。用c语言
可以使用暴力枚举的方法,从字符串的第一个字符开始,依次判断相邻两个字符是否相同,如果相同则继续向后判断,直到找到一个不同的字符为止,这时就找到了一个相同的子串。然后继续从下一个字符开始寻找。
具体实现如下:
```c
#include <stdio.h>
#include <string.h>
int count_substring(char *str) {
int len = strlen(str);
int count = 0;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (str[j] != str[j-1]) {
break;
}
count++;
}
}
return count;
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
int count = count_substring(str);
printf("相邻字符相同的子串个数为:%d\n", count);
return 0;
}
```
在上面的代码中,`count_substring` 函数接收一个字符串指针作为参数,返回相邻字符相同的子串个数。首先获取字符串的长度,然后使用两层循环遍历字符串,从第一个字符开始,依次判断相邻两个字符是否相同,如果相同则继续向后判断,直到找到一个不同的字符为止,这时就找到了一个相同的子串。子串个数加一,然后继续从下一个字符开始寻找。最后返回子串个数即可。
在主函数中,首先提示用户输入一个字符串,然后调用 `count_substring` 函数计算相邻字符相同的子串个数,并输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)