69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#define MAXLINE 100 /*最大输入行长度*/
int getline(char line[],int max);
int strindex(char source[], char searchfor[]);
char pettern[] = "ould"; /* 待查找的模式 */
int main()
{
char line[MAXLINE];
int found = 0;
while (getline(line , MAXLINE) > 0)
if (strindex(line,pettern) >= 0){
printf ("字符串符合条件%s\n", line);
found++;
}
else
{
printf("字符串不符合条件\n");
}
system("pause");
return found;
}
/* GETLINE函数:将行保存到S中,并返回该行的长度 */
int getline(char s[], int lim)
{
char c;
int i;
i = 0;
while( --lim > 0 )
{
c=getchar();
if(c != '\n')
s[i++] = c;
else
break;
}
s[i++] = '\0';
return i;
}
/* strindex函数:返回T在S中的位置;若未找到则返回-1 */
int strindex(char s[], char t[])
{
int i,j,k;
for(i = 0; s[i] != '\n'; i++)
{
for (j=i,k=0;t[k]!='\0'&& s[j] == t[k]; j++, k++)
;
if( k > 0&& t[k] == '\0')
return i;
}
return -1;
}
if(c != '\n')
s[i++] = c;
else
break;
c=='\n'就跳出循环了#include<stdio.h>
#include<stdlib.h>
#define MAXLINE 100 /*最大输入行长度*/
int getline(char line[],int max);
int strindex(char source[], char searchfor[]);
char pettern[] = "ould"; /* 待查找的模式 */
int main()
{
char line[MAXLINE];
int found = 0;
while (getline(line , MAXLINE) > 0)
if (strindex(line,pettern) >= 0){
printf ("%s\n", line);
found++;
}
system("pause");
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' || c == EOF) //添加部分代码
//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]); j++, k++);
//if( k > 0&& t[k] == '\0')//不要k>0;
if(t[k] == '\0')
{
return i;
}
}
return -1;
}
//分给我把
int getline(char s[], int lim)
{
char c;
int i;
i = 0;
while( --lim > 0 )
{
c=getchar();
if(c != '\n')
s[i++] = c;
else
break;
}
s[i++] = '\0';
return i;
}