输入两个字符串,判断一个字符串是否为另一个的子串

静悠

关注

阅读 144

2022-01-20

源程序:

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

int main()
{
        char s1[32], s2[32];
        printf("Please in put s1[32]:\n");
        scanf("%s", s1);
        printf("Please in put s2[32]:\n");
        scanf("%s", s2);

        char *p1 = s1, *p2 = s2;

        int count = 0;
        int lengh = strlen(s2);
        while (*p2 != '\0')
        {
                while (*p1 != *p2 && *p1 != '\0')
                {
                        p1++;
                }
                if (*p1 == '\0')
                {
                        printf("NO and EXIT!\n");
                        return 0;
                }
                else
                {
                        count++;
                        p2++;
                }
        }

        if (count == lengh)
        {
                printf("YES!\n");
        }
        else
        {
                printf("NO!\n");
        }

        return 0;
}

运行结果截图:
在这里插入图片描述

精彩评论(0)

0 0 举报