By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,782 Members | 1,328 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,782 IT Pros & Developers. It's quick & easy.

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

P: n/a
Can anyone do it? ARMY1987- what say?

May 30 '07 #1
Share this Question
Share on Google+
17 Replies


P: n/a
On May 30, 10:00 am, Umesh <fraternitydispo...@gmail.comwrote:
Can anyone do it? ARMY1987- what say?
Please do not post homework.
Show the code.

thanks

May 30 '07 #2

P: n/a
>>>>"U" == Umesh <fr****************@gmail.comwrites:

UCan 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
--
Charlton Wilbur
cw*****@chromatico.net
May 30 '07 #3

P: n/a

"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@r19g2000prf.googlegr oups.com...
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().
Jun 1 '07 #4

P: n/a
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);
}

Jun 1 '07 #5

P: n/a
On Fri, 01 Jun 2007 23:40:59 -0000, di*****************@gmail.com
wrote:
>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
Jun 2 '07 #6

P: n/a
di*****************@gmail.com writes:
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 :-)

--
Ben.
Jun 2 '07 #7

P: n/a
di*****************@gmail.com wrote:
>
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>.
The test for fscanf should be "== 1".
Nothing makes cd follow ab.
Failure to return 0 (or EXIT_SUCCESS) at completion.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 2 '07 #8

P: n/a
CBFalconer <cb********@yahoo.comwrites:
di*****************@gmail.com wrote:
>>
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.

--
Ben.
Jun 2 '07 #9

P: n/a
CBFalconer <cb********@yahoo.comwrites:
di*****************@gmail.com wrote:
>>
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 2 '07 #10

P: n/a

"CBFalconer" <cb********@yahoo.comha scritto nel messaggio
news:46***************@yahoo.com...
di*****************@gmail.com wrote:
>>
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.
Jun 2 '07 #11

P: n/a
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"?
Jun 3 '07 #12

P: n/a
ph******@alphalink.com.au.NO.SPAM (Peter 'Shaggy' Haywood) writes:
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.
And yet you are the only one doing so on this mornings news feed. Well done.
Jun 3 '07 #13

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

Jun 3 '07 #14

P: n/a
In article <11**********************@z28g2000prd.googlegroups .com>,
Umesh <fr****************@gmail.comwrote:
>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
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Jun 3 '07 #15

P: n/a
On Jun 3, 3:54 pm, Umesh <fraternitydispo...@gmail.comwrote:
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

Jun 4 '07 #16

P: n/a
"blufox" writes:
On Jun 3, 3:54 pm, Umesh <fraternitydispo...@gmail.comwrote:
>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.
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!
Jun 4 '07 #17

P: n/a
On Jun 4, 8:32 pm, "osmium" <r124c4u...@comcast.netwrote:
"blufox" writes:
On Jun 3, 3:54 pm, Umesh <fraternitydispo...@gmail.comwrote:
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.

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

Jun 5 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.