String Matching

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;
}
 
C

CBFalconer

Kelly said:
.... snip ...

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()?

Try ggets(), available at:

<http://cbfalconer.home.att.net/download/>

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
M

Mike Wahler

Kelly B said:
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)


for (yb = t; *yb !='\0'; ++yb)
if (memcmp(s,t,strlen(t)) == 0)

if (memcmp(s,yb,strlen(yb)) == 0)
return 0;
return -1;
}

-Mike
 
R

Richard Heathfield

Kelly B said:
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()?

Yes, there is. In fact, there are (at least) two approaches, one of
which is part of standard C.

The standard C alternative is called fgets - prototyped in <stdio.h> -
which solves the problem by letting you specify the array size and then
refusing to overflow it. If too much data is available, the excess is
left in stdin so that it can be retrieved by later calls.

The alternative approach is to write a routine which dynamically expands
the buffer as required during input capture. Several people have
written such routines, and distinguishing between them - at least,
between the ones that actually work - is mostly a matter of interface
design preferences.

For example, my own fgetline (and fgetword) can be found at
http://www.cpax.org.uk/prg/writings/fgetdata.php along with a
discussion of the issue.

Chuck's ggets can be found here:
http://cbfalconer.home.att.net/download/ggets.zip

Morris Dovey's getsm can be found here:
http://www.iedu.com/mrd/c/getsm.c

Eric Sosman's getline can be found here:
http://www.cpax.org.uk/prg/portable/c/libs/sosman/index.php

(I host it for him, that's all - blame him, not me.)
 
J

jaysome

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[])


Identifiers beginning with str[a-z], and then only at file scope, are
reserved for the implementation. Only "str" (or "mem", "is", or
"to"), immediately followed by a lower case letter, fall into this
category of off limits identifiers. Your identifier "strindex"
violates this. To not violate the C standard, you could change your
identifier to str_index.

Actually, if you use one or more underscores in an identifier, you're
pretty safe from ever running into conflicts with current and future
implementations and standards. Although not strictly legal, an
identifier like "string_index" will always and forever, IMHO, never
conflict with any existing or future implementation or standard.

Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top