#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getlines(char line[],int max);
int strindex(char source[],char searchfor[]);
char pattern[] = "ould"; /*pattern to search for */
/*find all lines matching pattern */
int main(void)
{
char line[MAXLINE];
int k;
int found = 0;
while(getlines(line,MAXLINE) > 0){
if((k = strindex(line,pattern)) >= 0)
printf("%c",line[k]);
found++;
}
else
printf("%d\n",k);
}
return found;
}
int getlines(char s[],int lim)
{
int i,c;
i = 0;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int strindex(char *s,char *t)
{
int i,j,k;
for(i = strlen(s) - 1; i >= 0; i--){
for(j = i,k = strlen(t)-1; k >= 0 && s[j] == t[k];k--,j--)
;
if(k < 0)
return (j+1);
}
return -1;
}
#include <string.h>
#define MAXLINE 1000
int getlines(char line[],int max);
int strindex(char source[],char searchfor[]);
char pattern[] = "ould"; /*pattern to search for */
/*find all lines matching pattern */
int main(void)
{
char line[MAXLINE];
int k;
int found = 0;
while(getlines(line,MAXLINE) > 0){
if((k = strindex(line,pattern)) >= 0)
{
printf("%s\n%d\n",line,k);
for(; line[k] != '\0';k++)printf("%c",line[k]);
found++;
}
else
printf("%d\n",k);
}
return found;
}
int getlines(char s[],int lim)
{
int i,c;
i = 0;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int strindex(char *s,char *t)
{
int i,j,k;
for(i = strlen(s) - 1; i >= 0; i--){
for(j = i,k = strlen(t)-1; k >= 0 && s[j] == t[k];k--,j--)
;
if(k < 0)
return (j+1);
}
return -1;
}
本文介绍了一个使用C语言实现的简单程序,该程序通过自定义的字符串匹配函数来查找特定模式在输入文本中的出现位置,并展示如何遍历匹配到的内容。通过对每行输入进行检查,该程序能够有效地定位并打印出包含指定模式的所有行及其位置。
2010

被折叠的 条评论
为什么被折叠?



