How to extract words containing 'ab' & 'cd' in a text file?

C

Charlton Wilbur

U> Can anyone do it? ARMY1987- what say?

If you have to ask, it's obvious that not anyone can do it, but as I
can do it, I know someone can.

Perhaps you should attempt it and see what you come up with.

Charlton
 
A

Army1987

Umesh said:
Can anyone do it? ARMY1987- what say?
What? Why am *I* supposed to do that?

After having opened the files, etc., etc...

while (read a word)
if (the word contains "ab" && the word contains "cd")
write the word;

close the files, etc., etc...

Look up for the %s format for fscanf(), and for strstr().
 
D

dimitris.papastamos

This should do it...

int main(void) {
FILE *open;
char word[64]

open = fopen("file.txt", "r");
if(!open) return -1;

while(fscanf(open, "%s", word) != EOF) {
if((strstr(word, "ab") != NULL) || (strstr(word, "cd") !=
NULL)) {
printf("gotcha [%s] !\n", word);
}
}

fclose(open);
}
 
B

Barry Schwarz

This should do it...

int main(void) {
FILE *open;
char word[64]

open = fopen("file.txt", "r");
if(!open) return -1;

while(fscanf(open, "%s", word) != EOF) {
if((strstr(word, "ab") != NULL) || (strstr(word, "cd") !=
NULL)) {

The word "about" satisfies the if and will be printed but it does not
meet the criteria specified in the subject. Only words containing ab
AND cd should be extracted.
printf("gotcha [%s] !\n", word);
}
}

fclose(open);
}


Remove del for email
 
B

Ben Bacarisse

This should do it...

int main(void) {
FILE *open;
char word[64]

open = fopen("file.txt", "r");
if(!open) return -1;

while(fscanf(open, "%s", word) != EOF) {

At least write: fscanf(open, "%63s", word);
The %s format with no field width is a Really Bad Idea(TM).
if((strstr(word, "ab") != NULL) || (strstr(word, "cd") !=
NULL)) {
printf("gotcha [%s] !\n", word);
}
}

fclose(open);
}

The other errors are left as an exercise to the reader :)
 
B

Ben Bacarisse

CBFalconer said:
This should do it...

int main(void) {
FILE *open;
char word[64]

open = fopen("file.txt", "r");
if(!open) return -1;

while(fscanf(open, "%s", word) != EOF) {
if((strstr(word, "ab") != NULL) || (strstr(word, "cd") !=
NULL)) {
printf("gotcha [%s] !\n", word);
}
}
fclose(open);
}

I suggest you at least try code you suggest, or mark it untested.

Obvious Faults:
return -1 is illegal.

You are very harsh! Returning an int != 0, EXIT_SUCCESS or
EXIT_FAILURE is implementation defined so one might call it ill
advised, but "illegal"?
Use EXIT_FAILURE and #include <stdlib>.
The test for fscanf should be "== 1".

I agree as a general rule (well, testing for n successful matches) but
in the case of a lone "%s" is it an error? I can't see how a
conforming fscanf can return anything but EOF or 1. Even as I type
this I can see library writers checking their code for paths that
return 0 for this format.
Nothing makes cd follow ab.

The rather limited specification did not require this.
 
K

Keith Thompson

CBFalconer said:
This should do it...

int main(void) {
FILE *open;
char word[64]

open = fopen("file.txt", "r");
if(!open) return -1;

while(fscanf(open, "%s", word) != EOF) {
if((strstr(word, "ab") != NULL) || (strstr(word, "cd") !=
NULL)) {
printf("gotcha [%s] !\n", word);
}
}
fclose(open);
}

I suggest you at least try code you suggest, or mark it untested.

I see no evidence that he didn't try it; the errors you point out are
ones that could easily be missed by insufficiently careful testing.
Obvious Faults:
return -1 is illegal. Use EXIT_FAILURE and #include <stdlib>.

return -1 is perfectly legal; it's merely non-portable.
The test for fscanf should be "== 1".

Yes, it should, but fscanf with a "%s" option reads the next
space-delimited word, and it will return the value EOF when it reaches
the end of the file. As far as I tell, it will never return a value
other than 1 or EOF. Just checking for EOF is poor style, but it
happens to work in this case.
Nothing makes cd follow ab.

Is that required? The only problem statement I've seen is in the
subject header: "How to extract words containing 'ab' & 'cd' in a text
file?". This could mean either
Extract words containing 'ab' and words containing 'cd'
or
Extract words containig both 'ab' and 'cd'

In either case, I see no implication of a required order (though the
problem statement is bad enough that that may well have been the
intent).
Failure to return 0 (or EXIT_SUCCESS) at completion.

Which happens to be harmless on many systems.
 
A

Army1987

CBFalconer said:
This should do it...

int main(void) {
FILE *open;
char word[64]

open = fopen("file.txt", "r");
if(!open) return -1;

while(fscanf(open, "%s", word) != EOF) {
if((strstr(word, "ab") != NULL) || (strstr(word, "cd") !=
NULL)) {
printf("gotcha [%s] !\n", word);
}
}
fclose(open);
}

I suggest you at least try code you suggest, or mark it untested.

Obvious Faults:
return -1 is illegal. Use EXIT_FAILURE and #include <stdlib>. s/illegal/implementation-defined
The test for fscanf should be "== 1".
What else could it be, other than 1 and EOF?
Nothing makes cd follow ab. So what?
Failure to return 0 (or EXIT_SUCCESS) at completion.
In C99 this is allowed.

And you didn't even say that it should be "%63s", that it prints
words containing "ab" OR "cd".

Then (not an error, but...):
the test strstr(word, "ab") != NULL hurts my eyes. We are merely
using strstr as a boolean expression, emphatising it is a pointer
is not very useful, also considering that he used if (!open), and
there if (open == NULL) *does* "look better", and does *not*
uselessly make a line 76 characters long, causing it to be wrapped.
 
P

Peter 'Shaggy' Haywood

Trolling square Umesh was jivin' on 29 May 2007 22:00:17 -0700 in
comp.lang.c.
How to extract words containing 'ab' & 'cd' in a text file?'s a bad
trip that Umesh has trolled before! Dig it!
Can anyone do it? ARMY1987- what say?

Ladies & gentlemen, please do not respond to trolls. Ignore them and
they will go away.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
R

Richard

Trolling square Umesh was jivin' on 29 May 2007 22:00:17 -0700 in
comp.lang.c.
How to extract words containing 'ab' & 'cd' in a text file?'s a bad
trip that Umesh has trolled before! Dig it!


Ladies & gentlemen, please do not respond to trolls. Ignore them and
they will go away.

And yet you are the only one doing so on this mornings news feed. Well done.
 
U

Umesh

How to modify the program so that it can extact words starting with
'ab', ending with 'cd' but not containing 'ef'?
 
W

Walter Roberson

How to modify the program so that it can extact words starting with
'ab', ending with 'cd' but not containing 'ef'?

The state machine approach I showed earlier can handle -all- of the
text processing problems that you have posed so far. Did you
try it? Did you research state machines? Once you know how to use
state machines, you will know how to sit down and rattle off the
solution to each of these kinds of questions in about 5 minutes.

State 0: 'a' -> State 1; '\n' -> State 0; otherwise -> State 99
State 1: 'b' -> State 2; '\n' Discard word -> State 0; otherwise -> State 99
State 2: 'c' -> State 3; '\n' Discard word -> State 0; 'e' -> State 9;
otherwise -> State 2
State 3: 'd' -> State 4; '\n' Discard word -> State 0; 'e' -> State 9;
otherwise -> State 2
State 4: '\n' Output word, Discard word -> State 0; 'e' -> State 9;
otherwise -> State 2
State 9: 'f' -> State 99; '\n' Discard word -> State 0;
otherwise -> State 2
State 99: '\n' Discard word -> State 0; otherwise -> State 99
 
B

blufox

How to modify the program so that it can extact words starting with
'ab', ending with 'cd' but not containing 'ef'?

Hi Umesh,

How will you write a program which accepts a word from user and prints
some "blah blah" if it starts with "ab".
similiarly how will you write a program which will print "foo bar"
when it finds "cd" as the last two letters in the word.
and on the similar lines how will you write a program which will do
nothing which finds "ef" anywhere in the word?

Combine all the three tests together.
HTH
Thanks
 
O

osmium

blufox said:
Hi Umesh,

How will you write a program which accepts a word from user and prints
some "blah blah" if it starts with "ab".
similiarly how will you write a program which will print "foo bar"
when it finds "cd" as the last two letters in the word.
and on the similar lines how will you write a program which will do
nothing which finds "ef" anywhere in the word?

Combine all the three tests together.

As far as I can tell umesh sees the world as consisting of a blur of
amorphous things: words and strings and lines and they seem to be synonyms
of some sort or other. Until he is willing to sit down and decide on
*definitions* for these three things that have some *invariant* meaning, he
is doomed to wander in the wilderness.

He should finish this table:

word - A word is ...
string - A string is ...
line - A line is ....

He seems to be searching for a needle in a haystack but, unfortunately, he
has no means of identifying what a needle is!
 
B

blufox

As far as I can tell umesh sees the world as consisting of a blur of
amorphous things: words and strings and lines and they seem to be synonyms
of some sort or other. Until he is willing to sit down and decide on
*definitions* for these three things that have some *invariant* meaning, he
is doomed to wander in the wilderness.

He should finish this table:

word - A word is ...
string - A string is ...
line - A line is ....

He seems to be searching for a needle in a haystack but, unfortunately, he
has no means of identifying what a needle is!
Because, unfortunately he is sitting on the needle.
Umesh, apply simple primary school mathematics to figure out how do
you read a word letter by letter.

Will help, may be...

thanks
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top