12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
编程知识 时间:2024-12-05 09:51:43
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在C语言中,可以使用strchr()函数或者自定义函数来查找指定字符。使用strchr()函数:#include <stdio.h>#include <string.h>int main() {char str[] = "Hello, World!";char ch
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在C语言中,可以使用strchr()函数或者自定义函数来查找指定字符。
#include <stdio.h>#include <string.h>int main() {char str[] = "Hello, World!";char ch = 'o';char *result;result = strchr(str, ch);if (result != NULL) {printf("Found '%c' at position %ld\n", ch, result - str);} else {printf("'%c' not found in the string\n", ch);}return 0;}
#include <stdio.h>int findChar(char *str, char ch) {int i;for (i = 0; str[i] != '\0'; i++) {if (str[i] == ch) {return i;}}return -1;}int main() {char str[] = "Hello, World!";char ch = 'o';int result;result = findChar(str, ch);if (result != -1) {printf("Found '%c' at position %d\n", ch, result);} else {printf("'%c' not found in the string\n", ch);}return 0;}
这两种方法都可以用来查找指定字符在字符串中的位置。第一种方法使用了标准库函数strchr(),而第二种方法则是自定义了一个函数来实现。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19