473,765 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Can anyone do it? ARMY1987- what say?

May 30 '07
17 2900

"CBFalconer " <cb********@yah oo.comha scritto nel messaggio
news:46******** *******@yahoo.c om...
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(op en, "%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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Jun 3 '07 #12
ph******@alphal ink.com.au.NO.S PAM (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************ **********@z28g 2000prd.googleg roups.com>,
Umesh <fr************ ****@gmail.comw rote:
>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 <fraternitydisp o...@gmail.comw rote:
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 <fraternitydisp o...@gmail.comw rote:
>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...@com cast.netwrote:
"blufox" writes:
On Jun 3, 3:54 pm, Umesh <fraternitydisp o...@gmail.comw rote:
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
1940
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
3180
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")) == NULL) {
3
1347
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
3128
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. Hope it not too simplistic for posting on thie newsgroup: Private Overloads Function CapWords(ByVal textstring As String, ByRef Delimiters() As Char) As String If textstring Is Nothing OrElse textstring.Length <= 0 Then Return Nothing If...
7
2896
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 given word. In example from:
7
4628
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 BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
34
2748
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 constant: i.e. five or the string is abc????? xyz How can i generalize it for any length of the string?
1
4439
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 the pictures. Where does wmp store all the pictures? Is there any function to extract the images or any useful c# code? Thanks
0
1439
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 varies in length, I cannot use standard text parsing excel functions to extract the product from the text. I could use text to columns but that gets labor intensive. Is there a way to extract out the capitalized words from text in Excel? How do I...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
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
3
2805
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.