c语言编写函数,从给定字符串中查找某指定的字符。 函数接口定义: int cfind ( char s[], char c ); 裁判测试程序样例: #include <stdio.h> #include <string.h> int cfind ( char s[], char c ); int main() { char cc,str[80]; int index; gets(str); scanf("%c",&cc); index=cfind(str,cc); if (index>=0) printf("%d",index); else printf("Not Found"); } /* 请在这里填写答案 */ 输入样例1: student d 输出样例1: 3 输入样例2: student x 输出样例2: Not Found
时间: 2024-03-27 20:40:11 浏览: 26
以下是函数实现代码:
```c
int cfind(char s[], char c) {
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] == c) {
return i;
}
}
return -1;
}
```
函数的思路是遍历给定字符串 `s`,找到第一个等于指定字符 `c` 的位置,返回该位置的下标。如果遍历完整个字符串都没有找到匹配的字符,则返回 -1 表示未找到。
相关问题
C语言用指针实现对给定字符串查找给定的子串并输出第一个子串所在的位置。
以下是使用指针实现的C语言代码:
```c
#include <stdio.h>
#include <string.h>
int find_substring(char *str, char *substr) {
int len1 = strlen(str);
int len2 = strlen(substr);
int i, j;
for (i = 0; i <= len1 - len2; i++) {
for (j = 0; j < len2; j++) {
if (*(str+i+j) != *(substr+j)) {
break;
}
}
if (j == len2) {
return i;
}
}
return -1;
}
int main() {
char str[100];
char substr[100];
int pos;
printf("Enter a string: ");
scanf("%s", str);
printf("Enter a substring to find: ");
scanf("%s", substr);
pos = find_substring(str, substr);
if (pos == -1) {
printf("Substring not found in the given string.\n");
} else {
printf("Substring found at position %d in the given string.\n", pos);
}
return 0;
}
```
这段代码中,`find_substring` 函数接受两个指向字符数组的指针,分别表示要查找的字符串和子串。函数使用两个循环来查找子串在字符串中的位置,最终返回子串在字符串中第一次出现的位置,如果没有找到则返回 -1。
在 `main` 函数中,我们通过 `scanf` 函数获取要查找的字符串和子串,然后调用 `find_substring` 函数来查找子串在字符串中的位置,最后输出结果。
C语言用指针实现对给定字符串查找给定的子串并输出第一个子串所在的位置
可以使用指针和字符串库函数来实现这个功能。以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100], substr[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
printf("Enter a substring to search for: ");
fgets(substr, 100, stdin);
// Remove newline characters from input strings
strtok(str, "\n");
strtok(substr, "\n");
int pos = -1; // Initialize position to -1 (not found)
char *ptr = strstr(str, substr); // Find first occurrence of substring
if (ptr != NULL) {
pos = ptr - str; // Calculate position of substring
}
printf("Substring found at position %d\n", pos);
return 0;
}
```
首先,我们使用 `fgets()` 函数从标准输入中读取字符串和子串。然后,我们使用 `strtok()` 函数删除字符串中的换行符。接下来,我们使用 `strstr()` 函数在字符串中查找子串。如果找到了子串,则计算子串在字符串中的位置(即指针的差值),并将其存储在变量 `pos` 中。最后,我们输出子串的位置。如果未找到子串,则 `pos` 仍为 -1。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)