在终端输入多行信息并且查找需要的信息且打印出含有该信息的行(不使用库函数)

在终端输入多行信息,找出包含“ould”的行(也可以自定义其他的查找信息),并打印该行。
#include<stdio.h>
#include<assert.h>
#define LINE_MAX 1000
char *fine_sub_str(char *line, char *substr)//在不允许调用库函数的情况下自己写出相当于strstr的函数。
{
char *str1 = line;
char *str2 = substr;
char *start = NULL;
assert(line);
assert(substr);
if (*str2 == '\0')
{
return str1;
}
while (*str1)
{
start = str1;
while ((*str1) && (*str2) && (*str1 == *str2))
{
str1++;
str2++;
}
if (*str2 == '\0')
{
return start;
}
str2 = substr;
str1 = start + 1;
}
return NULL;
}
int getline(char line[], int limit)//执行输入字符串并保存的操作并且返回值用作while循环的条件
{
int ch = 0;
int i = 0;
while ((--limit) && (ch = getchar()) && (ch != '\n') && (ch != EOF))
{
line[i++] = ch;
}
if (ch == '\n')
{
line[i++] = '\n';
}
line[i] = '\0';
return i;
}
int main()
{
char line[LINE_MAX];
while (getline(line, LINE_MAX))
{
if (fine_sub_str(line, "ould"))
{
printf("%s", line);
}
}
system("pause");
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值