Search input string

L

lucky_therock

hey..i ma newbie to c programming...its just my 3rd program..so i
wanna know a code in which input string can be matched with strings in
another opened file....!!
 
C

Clever Monkey

lucky_therock said:
hey..i ma newbie to c programming...its just my 3rd program..so i
wanna know a code in which input string can be matched with strings in
another opened file....!!
So, what do you have so far?

- Determine your specifications (exact string match? partial matches?
find all matches or first one wins?)
- Sketch out some pseudo-code to work out your algorithm.
- Iteratively develop the code.

e.g., my naive approach:

- Process the args verbatim (spaces significant), converting to same
case (caring about locale) if we don't care about case and make this our
constant search string. Handle errors.
- Open the external file, and read in contents as text, forming what we
get as an array of strings (convert to same case [again, locale issues]
if necessary). Handle error on open, on read and on memory allocation(s).
- Iterate through the strings, matching with our search string. On a
match, do something (save something or set a flag or whatever). Repeat
until list exhausted, if necessary.

The devil is in the details, but the actual logic is pretty simple. Of
course, there is all sorts of room for improvement on what I've
described here.
 
J

jaysome

hey..i ma newbie to c programming...its just my 3rd program..so i
wanna know a code in which input string can be matched with strings in
another opened file....!!

Consider using the strcmp() function. From the current C standard:

7.21.4.2 The strcmp function

Synopsis
include <string.h>
int strcmp(const char *s1, const char *s2);

Description
The strcmp function compares the string pointed to by s1 to the string
pointed to by s2.

Returns
The strcmp function returns an integer greater than, equal to, or less
than zero, accordingly as the string pointed to by s1 is greater than,
equal to, or less than the string. The contents of ‘‘holes’’ used as
padding for purposes of alignment within structure objects are
indeterminate. Strings shorter than their allocated space and unions
may also cause problems in comparison.

Regards
 
N

Nick Keighley

On 26 Apr 2007 11:37:58 -0700, lucky_therock

Consider posting in standard English. It makes your posts easier to
read.

Consider using the strcmp() function. From the current C standard:

7.21.4.2 The strcmp function

Synopsis
include <string.h>
int strcmp(const char *s1, const char *s2);

Description
The strcmp function compares the string pointed to by s1 to the string
pointed to by s2.

Returns
The strcmp function returns an integer greater than, equal to, or less
than zero, accordingly as the string pointed to by s1 is greater than,
equal to, or less than the string. The contents of ''holes'' used as
padding for purposes of alignment within structure objects are
indeterminate. Strings shorter than their allocated space and unions
may also cause problems in comparison.

depending what the OP is trying to do, strstr() might be better
 

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

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top