473,386 Members | 1,621 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,386 software developers and data experts.

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

Can anyone do it? ARMY1987- what say?

May 30 '07 #1
17 2862
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
>>>>"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

"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
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
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
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
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
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
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

"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
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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sarah Akers | last post by:
GgF ----gL5cJ72EqiGIQ0SK65Rz Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <head> <style type=3D"text/css">.eyebrow { FONT-WEIGHT: bold; FONT-SIZE: 10px; TE=
12
by: Yandos | last post by:
Hello all, why is the file 257 bytes long in windows xp, when it contains only 256 characters?: #include <stdio.h> int main(void) { int i; FILE *out; if ((out = fopen("256.tmp", "w")) ==...
3
by: Giganews | last post by:
I'm facing a crazy problem: <% spc = "<abcd" response.write spc %> will not work, spc is empty. When I replace <abcd with ab<cd, then spc contains only ab.
5
by: Dennis | last post by:
I know this is probably a very overworked issue but thought I'd share the code below to convert words in a text string to capitalize the first letter of the word using an array of word delimiters. ...
7
by: teo | last post by:
hallo, I need to extract a word and few text that precedes and follows it (about 30 + 30 chars) from a long textual document. Like the description that Google returns when it has found a...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
34
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
1
by: Oscar | last post by:
Hi, Is there a way to extact the album cover image that is shown in WMP 11. I Would like to have a cover.jpg file in each album folder so I'm able to make a backup of all mp3's as well as all...
0
by: wbw | last post by:
I am trying to extract capitalized words from text in Excel. I have a list of a combination of brands and products and I am trying to extract out the product attribute from the text. Since the text...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...
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,...

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.