c语言 字符串包含函数
English Answer:
The strstr() Function.
The `strstr()` function in C language is used to find the first occurrence of a substring within a string. It takes two arguments:
1. A pointer to the string to be searched (haystack).
2. A pointer to the substring to be found (needle).
If the substring is found within the string, the function returns a pointer to the first character of the substring within the string. If the substring is not found, the function returns a null pointer (`NULL`).
Syntax:
char strstr(const char haystack, const char needle);
Return Value:
A pointer to the first occurrence of the substring within the string, or `NULL` if the substring is not found.
Example:
C.
#include <stdio.h>。
#include <string.h>。
int main()。
{。
char haystack[] = "Hello, world!";
char needle[] = "world";
字符串函数定义 char result = strstr(haystack, needle);
if (result != NULL)。
{。
printf("Substring found at position %d\n", result haystack);
}。
else.
{。
printf("Substring not found\n");
}。
return 0;
}。
Output:
Substring found at position 7。
The strchr() Function.
The `strchr()` function in C language is used to find the first occurrence of a character within a string. It takes two arguments:
1. A pointer to the string to be searched (haystack).
2. The character to be found (needle).
If the character is found within the string, the function returns a pointer to the first occurrence of the character within the string. If the character is not found, the function returns a null pointer (`NULL`).
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论