欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[C语言]在终端输入多行信息,找出包含“ould”的行,并打印改行

程序员文章站 2022-06-22 12:07:01
#define _crt_secure_no_warnings 1 #include #include #include<...
#define _crt_secure_no_warnings 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#define max 1000
 
char *my_strstr(const char *dst,const  char *src)
{
    assert(dst);
    assert(src);
    char *p = dst;
    char *s1 = p;
    char *s2 = src;
    while (*s1)
    {
        s1 = p;
        s2 = src;
        while ((*s1!='\0')&&(*s2 != '\0'))
        {
            if (*s1++ == *s2++)
            {
                ;
            }
            else
            {    
                p++;
                break;
            }
        }
        if (*s2 == '\0')
        {
            return p;
        }
    }
    return null;
}
int getline(char *line, int limit)
{
    assert(line);
    char ch = 0;
    int i = 0;
    while (limit-- && ((ch = getchar()) != eof) && ch != '\n')
    {
        line[i++] = ch;
    }
    if (ch == '\n')
    {
        line[i++] = '\n';
    }
    line[i] = '\0';
    return i;
}
int main()
{
    /*char *str1 = "abbbcdef";
    char *str2 = "bbcd";
    char *ret = my_strstr(str1, str2);
    printf("%s\n", ret);*/
    char *str1 = "ould";
    char line[max] = { 0 };
    while (getline(line, max - 1))
    {
        if (my_strstr(line, str1))
        {
            printf("%s\n", line);
        }
    }
    system("pause");
    return 0;
}