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(commen ted 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,str len(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(l ine,MAXLINE,fp) >0)
{
if(strindex(lin e,pattern)>=0)
{
printf("%s",lin e);
found ++;
}
}
if(found==0)
printf("String Not Found\n");
fclose(fp);
return 0;
} 4 3950
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.securityfoc us.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
"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(commen ted 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,str len(t)) == 0)
if (memcmp(s,yb,st rlen(yb)) == 0)
return 0;
return -1;
}
-Mike
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.
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(commen ted 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<stdli b.h> #include<strin g.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_ind ex" will always and forever, IMHO, never
conflict with any existing or future implementation or standard.
Best regards
--
jay This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 files from gif
# format to png format. Now you need to change the
# html code to use the .png files. So, essentially
|
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 parsed can completely wreck it.
The string I am trying to parse is as follows:
commandText=insert into (Text) values (@message + N': ' +...
|
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 tool for
breaking ties when a phonic call returns a bunch of possibilities.
Also, I'm looking for someone that has a zip code file with alternate...
|
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 find one.
|
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 library?
| |
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 have a list, as
below:
sentence = "the color is $red"
patterns =
pos = sentence.find($)
|
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 T. Smith" "Fred Barzowsky"
"Department of Oncology" "Office of Student Affairs" "Lewis Hall" etc. etc.
etc.
I am wondering what the efficient way...
|
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 characters or simply matching a single character) :
ANSI-89 - Mainly used only by Jet / ACE SQL
ANSI-92 - Mainly used by SQL Server and other grown-up...
|
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 "?",
where "*" matches zero or more consecutive occurrences of any
character and "?" matches a single occurrence of any character.
Does boost or some...
|
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, given the string 123456789
I'd like to use the RE ((2)|(4)){3} to get the following matches:
2345
4567
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |