3、 编写函数int findsub(char *str1,char *str2),功能是返回字符串str2在字符串str1中首先出现的字符位置。
时间: 2024-05-06 08:18:25 浏览: 20
下面是实现该函数的代码:
```c
int findsub(char *str1, char *str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len2 > len1) {
return -1; // 如果要查找的字符串长度大于目标字符串长度,则不存在
}
for (int i = 0; i < len1 - len2 + 1; i++) {
bool found = true;
for (int j = 0; j < len2; j++) {
if (str1[i + j] != str2[j]) {
found = false;
break;
}
}
if (found) {
return i; // 如果找到了,则返回位置
}
}
return -1; // 找不到,返回-1
}
```
该函数的思路是:先判断要查找的字符串长度是否大于目标字符串长度,如果是,则不存在,直接返回-1。然后从目标字符串的第一个字符开始,逐个判断是否与要查找的字符串相同,如果全部相同,则说明找到了,返回其位置;如果一旦发现不同,则继续从下一个位置开始查找,直到查找完整个目标字符串为止。如果最终还是找不到,则返回-1。
相关问题
用c语言编写函数 int StrCout ( char *str1, char *str2),其功能是统计字符串str2在str1中出现的次数,返回该数值。要求:在主函数中输人str1和str2,调用函数 StrCount (str1,str2)后输出结果
下面是一个示例实现:
```c
#include <stdio.h>
#include <string.h>
int StrCount(char *str1, char *str2) {
int count = 0;
size_t len1 = strlen(str1);
size_t len2 = strlen(str2);
for (int i = 0; i < len1 - len2 + 1; i++) {
if (strncmp(str1 + i, str2, len2) == 0) {
count++;
}
}
return count;
}
int main() {
char str1[100], str2[100];
printf("请输入字符串1:");
scanf("%s", str1);
printf("请输入字符串2:");
scanf("%s", str2);
int count = StrCount(str1, str2);
printf("字符串2在字符串1中出现的次数为:%d\n", count);
return 0;
}
```
这个函数首先通过 `strlen` 函数获取两个字符串的长度,然后通过循环遍历字符串1中所有可能的子串,使用 `strncmp` 函数来比较子串和字符串2是否相等,并统计出现次数。主函数中通过 `scanf` 函数获取两个字符串,调用 `StrCount` 函数并输出结果。
用C语言实现类似atoi函数,把字符串“123456”转换成数值123456 int atoi (char *str)
你可以使用以下代码实现类似atoi函数的功能:
```c
#include <stdio.h>
int atoi(char *str) {
int result = 0;
int sign = 1;
int i = 0;
// 处理空格
while (str[i] == ' ') {
i++;
}
// 处理正负号
if (str[i] == '-' || str[i] == '+') {
sign = (str[i++] == '-') ? -1 : 1;
}
// 转换数字
while (str[i] >= '0' && str[i] <= '9') {
result = (result * 10) + (str[i++] - '0');
}
return sign * result;
}
int main() {
char str[] = "123456";
int num = atoi(str);
printf("%d\n", num);
return 0;
}
```
此代码中的atoi函数会将字符串转换为整数,根据字符串中的字符来计算结果。它会跳过前导空格,处理正负号,并将字符串中的数字字符转换为相应的整数值。最后,它会返回转换后的整数值。在main函数中,我们使用字符串"123456"作为示例,并打印转换后的结果。
相关推荐
![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_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)