首页 > 其他 > 详细

函数与程序结构

时间:2019-11-16 11:31:24      阅读:61      评论:0      收藏:0      [点我收藏+]

#include <stdio.h>
#define MAXLINE 1000 /*最大输入行长度*/
int getline(char line[], int max);
int strindex(char source[], char searchfor[]);
char pattern[] = ‘ould‘; /*待查找的模式*/
/*找出所有与模式匹配的行*/
main()
{
 char line[MAXLINE];
 int found = 0;
 while(getline(line,MAXLINE)>0)
  if(strindex(line,pattern)>=0)
  {
   printf("%s",line);
   found++;
  }
 return found;
}
/*getline函数:将行保存到s中,并返回该行的长度*/
int getline(char s[], int lim)
{
 int c, i;
 i = 0;
 while(--lim>0 && (c=getchar()) != EOF && c!=‘\n‘)
  s[i++] = c;
 if(c == ‘\n‘)
  s[i++] = c;
 s[i] = ‘\0‘;
 return i;
}
/*strindex函数:返回t在s中的位置,若未找回则返回-1*/
int strindex(char s[], char t[])
{
 int i, j, k;
 for(i = 0;s[i]!=‘\0‘;i++)
 {
  for(j=i,k=0;t[k]!=‘\0‘ && s[j] == t[k];k++,k++)
   ;
  if(k>0 && t[k] == ‘\0‘)
   return i;
 }
 return -1;
}
 
 
 
 
 

函数与程序结构

原文:https://www.cnblogs.com/TheFly/p/11871054.html

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