查找子串,补充下面的程序 #include<stdio.h> #include<string.h> int fun(char *s,char*t) { // } int main() { char str1[30],str2[10]; gets(str1); gets(str2); printf("%d",fun(str1,str2)); return 0; }
时间: 2024-01-12 18:02:43 浏览: 72
可以使用字符串库函数strstr来查找子串,具体实现如下:
```c
#include<stdio.h>
#include<string.h>
int fun(char *s, char *t) {
char *p = strstr(s, t); // 在s中查找t
if (p == NULL) { // 如果找不到,返回-1
return -1;
} else { // 如果找到,返回t在s中的起始位置
return p - s;
}
}
int main() {
char str1[30], str2[10];
gets(str1);
gets(str2);
printf("%d", fun(str1, str2));
return 0;
}
```
注意,gets()函数存在安全性问题,建议使用fgets()函数代替。
相关问题
*定义函数实现n个字符串按字典顺序排序 开头: #include<stdio.h> #include<string.h> void fun (char *str[],int n)
好的,以下是一个实现n个字符串按字典顺序排序的示例代码:
```
#include <stdio.h>
#include <string.h>
void fun(char *str[], int n) {
int i, j;
char *temp;
// 使用冒泡排序算法
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(str[j], str[j+1]) > 0) {
// 交换两个字符串的位置
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
}
int main() {
char *str[] = {"apple", "banana", "orange", "grape", "kiwi"};
int n = sizeof(str) / sizeof(str[0]);
fun(str, n);
printf("排序后的字符串数组:\n");
for (int i = 0; i < n; i++) {
printf("%s ", str[i]);
}
printf("\n");
return 0;
}
```
在这个示例代码中,我们使用了冒泡排序算法对字符串数组进行排序。在排序过程中,我们使用了 `strcmp()` 函数来比较两个字符串的大小关系,并使用指针变量 `temp` 来交换两个字符串在数组中的位置。最后,我们输出排好序的字符串数组。
请注意,这个示例代码中的字符串数组是在程序中直接定义的。如果您需要从用户输入中获取字符串数据,请使用 `scanf()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
#include <stdio.h> #include <string.h> void fun(char *s,int num) { } void main() { char s[10]; gets(s); fun(s,7); printf("%s", s); }
这是一道需要填空的题目,题目思路是将字符串 `s` 中的前 `num` 个字符反转。可以使用两个指针分别指向字符串的首位和末位,依次交换两个指针所指向的字符,直到两个指针相遇为止。具体实现可以这样写:
```c
#include <stdio.h>
#include <string.h>
void fun(char *s, int num) {
int i = 0, j = num - 1;
while (i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
void main() {
char s[10];
gets(s);
fun(s, 7);
printf("%s", s);
}
```
其中,`i` 和 `j` 分别表示指向字符串的首位和末位的指针,`temp` 是一个临时变量,用于交换两个指针所指向的字符。在循环中,每次将指针向中间移动一位,并交换两个指针所指向的字符,直到两个指针相遇为止。最终输出交换后的字符串即可。
阅读全文
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)