首页 > 其他 > 详细

在字符串中寻找目标字符串

时间:2016-03-22 06:39:17      阅读:180      评论:0      收藏:0      [点我收藏+]

在终端输入多行信息,找出包含“ould”的行,并打印改行。

如:

    Au,love could you and I with fate conspire

    To grasp this sorry scheme of things entire,

    Would not we shatter it to bitd – and then.

    在终端输出上述的文字,输出

    Au,love could you and I with fate conspire

    Au,love could you and I with fate conspire

    To grasp this sorry scheme of things entire,

    Would not we shatter it to bitd – and then.

    Would not we shatter it to bitd – and then.

#include <stdio.h>
#define MAX 1000
int getline(char line[])
{
	int limit = MAX - 1;
	int ch = 0;
	int i = 0;
	while ((ch = getchar()) && (--limit) && ch != ‘\n‘ && ch != EOF)
	{
		line[i] = ch;
		i++;
	}
	if (ch == ‘\n‘)
	{
		line[i++] = ‘\n‘;
	}
	line[i] = ‘\0‘;
	return i;
}
char *my_strstr(char line[], char *match)
{
	int i, j,k;
	for (i = 0; line[i] != ‘\0‘; i++)
	{
		for (j = 0,k =i; match[j] != ‘\0‘ && line[k] == match[j]; k++, j++)
		{
			;
		}
		if ((j > 0) && (match[j] == ‘\0‘))
		{
			return &line[i];
		}
	}
	return NULL;
}
int main()
{
	char line[MAX];
	char *p = "ould";
	while (getline(line))
	{
		if (my_strstr(line, p))
		{
			printf("%s", line);
		}
	}
	system("pause");
	return 0;
}


本文出自 “sunshine225” 博客,请务必保留此出处http://10707460.blog.51cto.com/10697460/1753589

在字符串中寻找目标字符串

原文:http://10707460.blog.51cto.com/10697460/1753589

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!