sse——字符串数组

//练习题2. 字符串数组
//输入两个长度小于100的字符串A和B,且A的长度大于B的长度,判断B是不是A的子串,是则输出”YES”,否则输出”NO”。注意:串中任意个连续的字符组成的子序列称为该串的子串。
//提示:不需要使用自定义函数实现,也不能使用已有的字符串查找子串函数
//输入:输入两行,第一行为字符串A,第二行为字符串B。
//输入提示信息:"Please input the first str:"
//输入格式:"%s"
//输入提示信息:"Please input the second str:"
//输入格式:"%s"
//如:
//Please input the first str: Abcdefghijk123
//Please input the second str: 123
//或
//Please input the first str: abefsfl
//Please input the second str: befs
//或
//Please input the first str: aAbde
//Please input the second str: abc
//输出:按题目要求输出一行,"YES" 或 "NO"。
//输出样例:
//YES
//或
//YES
//或
//NO
#include<stdio.h>
#include<string.h>
#define maxLen 100 //0.5
int main()
{                   
    char a[maxLen];
    char b[maxLen];
    printf("Please input the first str:");
    scanf("%s", a);
    getchar();
    printf("Please input the second str:");
    scanf("%s", b);
    int i = 0, j = 0;
    int ans = 0;
    while (i < strlen(a) - strlen(b) + 1)//1
    {                   
        if (j == strlen(b))
        {                   
            ans = 1;
            break;
        }
        if (a[i + j] == b[j])//1
            ++j;//1
        else
        {                   
            j = 0;
            ++i;//1
        }
    }
    if (ans)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}         

posted @ 2022-11-17 14:59  诩en  阅读(77)  评论(0)    收藏  举报