用C语言编写下题:输入一个无空格字符串和一个长度为2的子串,求该子串在该字符串中出现的次数,如果查找子串是两个相同的字符,那么计过次数的字母不再计入
时间: 2023-11-27 16:54:04 浏览: 127
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100], sub[3];
int len, sub_len, count = 0;
scanf("%s %s", str, sub);
len = strlen(str);
sub_len = strlen(sub);
if (sub_len == 2 && sub[0] == sub[1]) {
for (int i = 0; i < len - 1; i++) {
if (str[i] == sub[0] && str[i+1] == sub[1]) {
count++;
i++;
}
}
} else {
for (int i = 0; i < len - sub_len + 1; i++) {
if (strncmp(str+i, sub, sub_len) == 0) {
count++;
}
}
}
printf("出现次数:%d\n", count);
return 0;
}
```
相关问题
c语言编程:输入一个无空格字符串和一个长度为2的子串,求该子串在该字符串中出现的次数,如果查找子串是相同的2个字符,那么记过次数的字母不再计入。
好的,下面是代码实现:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], substr[3]; // 定义字符串和子串数组
int len, count = 0, i = 0, j; // 定义长度、计数器和循环变量
printf("请输入一个无空格字符串:");
fgets(str, sizeof(str), stdin); // 从标准输入中读取字符串
len = strlen(str);
printf("请输入一个长度为2的子串:");
fgets(substr, sizeof(substr), stdin); // 从标准输入中读取子串
// 遍历字符串
while (i < len - 1)
{
if (str[i] == substr[0] && str[i+1] == substr[1]) // 判断是否匹配子串
{
count++; // 匹配次数加1
i += 2; // 直接跳过子串
continue;
}
else if (substr[0] == substr[1] && (str[i] == substr[0] || str[i] == substr[1]))
{
i++; // 如果子串是相同的2个字符,那么跳过这个重复的字符
continue;
}
i++; // 继续遍历字符串
}
printf("子串在字符串中出现的次数为:%d\n", count);
return 0;
}
```
这里使用了 `fgets()` 函数来读取字符串和子串,因为它可以处理空格并且可以限制输入的长度。在遍历字符串时,使用了循环变量 `i`,并且每次增加的步长根据是否匹配子串来确定。如果子串是相同的两个字符,那么在匹配时需要跳过一个字符,否则会重复计数。
如何用C语言编写一个函数来计算一个长度为2的子串在一个主字符串中的重复次数?
可以使用C语言编写一个函数来计算一个长度为2的子串在一个主字符串中的重复次数。以下是一个可能的实现方法:
```c
#include <stdio.h>
#include <string.h>
int countSubstring(char *mainString, char *substring) {
int mainStringLength = strlen(mainString);
int substringLength = strlen(substring);
int count = 0;
for (int i = 0; i <= mainStringLength - substringLength; i++) {
if (strncmp(mainString + i, substring, substringLength) == 0) {
count++;
}
}
return count;
}
int main() {
char mainString[] = "Hello, world!";
char substring[] = "world";
int result = countSubstring(mainString, substring);
printf("The substring '%s' appears %d times in the main string '%s'.\n", substring, result, mainString);
return 0;
}
```
这个函数使用`strncmp`函数来比较主字符串和子字符串,如果它们相等,则计数器加一。最后,函数返回计数器的值,即子字符串在主字符串中出现的次数。在主函数中,我们使用一个示例字符串和子字符串调用这个函数,并打印结果。
请注意,这个实现方法假定子字符串只包含小写字母和空格。如果需要处理其他字符集,您可能需要使用更复杂的算法。此外,该实现方法没有考虑重复出现多次的情况,即只计算第一个出现的次数。如果您需要计算所有出现的次数,则需要稍微修改这个函数。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)