473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String Matching

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[i]='\0';
return i;
}

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')
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;
}

Apr 8 '07 #1
4 3944
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

Apr 8 '07 #2

"Kelly B" <ke****@invalid.comwrote in message
news:ev**********@aioe.org...
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[i]='\0';
return i;
}

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')
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
Apr 8 '07 #3
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/...sman/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.
Apr 8 '07 #4
On Sun, 08 Apr 2007 09:32:05 +0530, Kelly B <ke****@invalid.com>
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[i]='\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
Apr 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
0
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
10
by: javuchi | last post by:
I'm searching for a library which makes aproximative string matching, for example, searching in a dictionary the word "motorcycle", but returns similar strings like "motorcicle". Is there such a...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
0
NeoPa
by: NeoPa | last post by:
ANSI-89 v ANSI-92 Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
1
by: Ben | last post by:
I apologize in advance for the newbie question. I'm trying to figure out a way to find all of the occurrences of a regular expression in a string including the overlapping ones. For example,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.