溫馨提示×

c語言怎么在字符串中查找字符串

小億
115
2024-09-05 06:19:49
欄目: 編程語言

在C語言中,可以使用strstr()函數在一個字符串中查找另一個字符串

#include<stdio.h>
#include<string.h>

int main() {
    char haystack[] = "Hello, welcome to the world of programming!";
    char needle[] = "world";

    char *result = strstr(haystack, needle);

    if (result) {
        printf("Found the string '%s' in the text '%s'.\n", needle, haystack);
    } else {
        printf("The string '%s' was not found in the text '%s'.\n", needle, haystack);
    }

    return 0;
}

在這個示例中,我們定義了兩個字符串變量haystackneedle。然后,我們使用strstr()函數在haystack中查找needle。如果找到了匹配的子字符串,strstr()函數將返回一個指向該子字符串的指針,否則返回NULL。我們使用if語句檢查結果,并根據是否找到子字符串打印相應的消息。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女