如何从 n 个字符串中查找子字符串

How to find sub string from n strings

本文关键字:字符串 查找      更新时间:2023-10-16

我正在尝试制作一个具有N个数组的程序,该程序搜索两个字符串的匹配字符,然后将子字符串与其余字符串进行比较。代码如下:

int main ()
{
    int a, b, n;
    char sir[50];
    printf("Number of strings: "); scanf("%d", &n);
    if(n<=1){
        printf("The program cannot run without at least 2 strings!");
    } else {
        printf("The program will run for %d strings.n", n);
        printf("nString number 1: ");
        scanf("%s", &sir);
        std::string first(sir);
        cout << first;
        for(a=2; a<=n; ){
            printf("nString number %d: ", a);
            scanf("%s", &sir);
            std::string temp(sir);
            if(!!!first.contains(temp)!!!){
                a++;
            } else {
            printf("Program stops the substring doesn't match with the last string.");
            return 0;
            }
        }
    }
}

我把!!放在哪里,我不知道如何编码那部分。

只是为了回答你的问题:以下行应该可以完成这项工作:

if(first.find(temp) != std::string::npos)