String Matching

Discussion in 'C Programming' started by Kelly B, Apr 8, 2007.

  1. Kelly B

    Kelly B Guest

    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;
    }
    Kelly B, Apr 8, 2007
    #1
    1. Advertising

  2. Kelly B

    CBFalconer Guest

    Kelly B wrote:
    >

    .... 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


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Apr 8, 2007
    #2
    1. Advertising

  3. Kelly B

    Mike Wahler Guest

    "Kelly B" <> wrote in message
    news:ev9pfv$q99$...
    > 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
    Mike Wahler, Apr 8, 2007
    #3
  4. 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.)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Apr 8, 2007
    #4
  5. Kelly B

    jaysome Guest

    On Sun, 08 Apr 2007 09:32:05 +0530, Kelly B <>
    wrote:

    >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
    --
    jay
    jaysome, Apr 8, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seth
    Replies:
    1
    Views:
    1,076
    Ray Andraka
    Aug 1, 2003
  2. Replies:
    1
    Views:
    597
    Craig Deelsnyder
    Oct 25, 2003
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,298
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    233
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    227
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page