K
Kelly B
This is a simple string matching code from K&R2 (modified to match a
user entered string from a text file)
I tried to code an alternative strindex function(commented below) but it
does not work ,i don't seem to understand where exactly am i going wrong
Secondly using gets() can be a dangerous option if the user enters more
characters than the array can hold.is there a safer way out of using gets()?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLINE 1000
int getline(char s[],int lim,FILE *fp)
{
int i=0,ch;
while(--lim >0 && (ch=fgetc(fp))!=EOF && ch!='\n')
s[i++]=ch;
if(ch=='\n')
s[i++]=ch;
s='\0';
return i;
}
int strindex(char s[],char t[])
{
int i,j,k;
for(i=0;s!='\0';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;
}
/*This function doesn't work..the earlier one from K&R2 is fine,can
anyone please tell me where am i going
wrong? */
/*int strindex (char *s,char *t)
{
char *yb;
for (yb = t; *t !='\0'; ++t)
if (memcmp(s,t,strlen(t)) == 0)
return 0;
return -1;
}
*/
int main(void)
{
FILE *fp;
char line[MAXLINE];
int found=0;
char pattern[100];
if((fp=fopen("C:\\foo.txt","r"))==NULL)
{
printf("File does not exist\n");
return 0;
}
printf("Enter the string to be searched\n");
gets(pattern);
/*buffer can easily overflow if i enter more than 100 characters
at runtime,is there a safe way to use gets*/
while(getline(line,MAXLINE,fp)>0)
{
if(strindex(line,pattern)>=0)
{
printf("%s",line);
found ++;
}
}
if(found==0)
printf("String Not Found\n");
fclose(fp);
return 0;
}
user entered string from a text file)
I tried to code an alternative strindex function(commented below) but it
does not work ,i don't seem to understand where exactly am i going wrong
Secondly using gets() can be a dangerous option if the user enters more
characters than the array can hold.is there a safer way out of using gets()?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLINE 1000
int getline(char s[],int lim,FILE *fp)
{
int i=0,ch;
while(--lim >0 && (ch=fgetc(fp))!=EOF && ch!='\n')
s[i++]=ch;
if(ch=='\n')
s[i++]=ch;
s='\0';
return i;
}
int strindex(char s[],char t[])
{
int i,j,k;
for(i=0;s!='\0';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;
}
/*This function doesn't work..the earlier one from K&R2 is fine,can
anyone please tell me where am i going
wrong? */
/*int strindex (char *s,char *t)
{
char *yb;
for (yb = t; *t !='\0'; ++t)
if (memcmp(s,t,strlen(t)) == 0)
return 0;
return -1;
}
*/
int main(void)
{
FILE *fp;
char line[MAXLINE];
int found=0;
char pattern[100];
if((fp=fopen("C:\\foo.txt","r"))==NULL)
{
printf("File does not exist\n");
return 0;
}
printf("Enter the string to be searched\n");
gets(pattern);
/*buffer can easily overflow if i enter more than 100 characters
at runtime,is there a safe way to use gets*/
while(getline(line,MAXLINE,fp)>0)
{
if(strindex(line,pattern)>=0)
{
printf("%s",line);
found ++;
}
}
if(found==0)
printf("String Not Found\n");
fclose(fp);
return 0;
}