小作业(一个字符串中包含几个另一个字符串)

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

#define Num 1000

int main()
{
    int i = 0,j = 0,k,count = 0,h = 0,c;
    char str1[Num],str2[Num],str3[Num];

    printf("Please input the first string,and enter as the end:\n");

    while((c = getchar()) != EOF && c != '\n')

    {
        str1[i] = c;
        i++;
    }

    printf("Please input the second string,and enter as the end:\n");

    while((c = getchar()) != EOF && c != '\n')
    {
        str2[j] = c;
        j++;
    }

    if(i < j)
    {
        printf("str1 has 0 str2");
    }

    else
        {
            for(k = 0;k < i - j + 1;k++)
            {
                for(h = 0;h < j;h++)
                {
                   str3[h] = str1[k + h];
                }

                if(strcmp(str3,str2) == 0)
                    count++;
            }
            printf("str1 has %d str2s",count);
        }

    return 0;
}


posted @ 2014-10-30 08:56  司空格子Ored  阅读(222)  评论(0编辑  收藏  举报