473,748 Members | 2,207 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 #1
17 2898
On May 30, 10:00 am, Umesh <fraternitydisp o...@gmail.comw rote:
Can anyone do it? ARMY1987- what say?
Please do not post homework.
Show the code.

thanks

May 30 '07 #2
>>>>"U" == Umesh <fr************ ****@gmail.comw rites:

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*****@chromat ico.net
May 30 '07 #3

"Umesh" <fr************ ****@gmail.comh a scritto nel messaggio
news:11******** **************@ r19g2000prf.goo glegroups.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(op en, "%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(op en, "%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(op en, "%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(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>.
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.securityfoc us.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********@yah oo.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(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.
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********@yah oo.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(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.
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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1937
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
3176
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
1345
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
3127
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
4626
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
2741
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
4438
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
1438
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
8991
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
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
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
6076
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
4607
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...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.