How can I use “s.find” in c++
时间: 2024-05-08 13:14:49 浏览: 94
In C, you can use the strstr() function to find the first occurrence of a substring within a string. The function takes two arguments, the string to search in and the substring to search for. It returns a pointer to the first occurrence of the substring, or NULL if the substring is not found. Here is an example:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
char substr[] = "world";
char *ptr = strstr(str, substr);
if (ptr != NULL) {
printf("Substring found at position %ld\n", ptr - str);
} else {
printf("Substring not found\n");
}
return 0;
}
```
Output:
```
Substring found at position 7
```
In this example, we are searching for the substring "world" within the string "Hello, world!". The strstr() function returns a pointer to the first occurrence of the substring, which we can use to calculate its position within the string. If the substring is not found, strstr() returns NULL.
阅读全文