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

search a file for a keyword and replace that keyword with a another word

write a program that search a file for a keyword and replace that
keyword with a another word. e.g. search for 'have' and replace with
'had' and list all the changes made ....to the screen...
Jul 27 '06 #1
5 4820
Banfa
9,065 Expert Mod 8TB
No thanks, but you feel free to have a go and when you have finished post your code here and we will help you debug it.

Loop up the following functions

File handling

fopen
fgets
fread
fwrite
fprintf
fclose

String and Memory Handling

strcmp
strncmp
memcmp
strcpy
memcpy

And finally look up array handling.
Jul 27 '06 #2
main()
{
char nm[20];
char SearchWord[20];
cout<<"enter Search word";
cin>>SearchWord;

// find the file and copy it into nm
strcpy(nm,system("grep SearchWord ./ "));

//It will search the word and Replace by ReplaceWord
system( "vi nm :/SearchWord/Replaceword/g");
}
Jul 31 '06 #3
Banfa
9,065 Expert Mod 8TB
I don't really think that constitues writing a program pkp_budha, you could actually do that with a shell script/batch file. Not to mention that is only going to work on systems that have grep and vi installed (so *nix most likely although they exist for Windows).
Jul 31 '06 #4
yadneo
1
hey u guys cant u help a little more clearly
c
i have entered a sentence and wont 2 find a word in it
heres my go bt can u modify it coz it wont work

...

...
int i=0,j=0,ag=0;

while(sentence[i]!='/0')
{
while(word[j]!='/0')
{
if((int)sent[i]==(int)word[j])
{
i++;
j++;
flag++;
}
else
{
i++;
j=0;
}
}
}

if(flag==strlen(word))
cout<<"word found";

else
cout<<"nt found";






}
Sep 16 '06 #5
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. ...
  2. int i=0,j=0,ag=0;
  3.  
  4. while(sentence[i]!='/0')
  5. {
  6.     while(word[j]!='/0')
  7.     {
  8.         if((int)sent[i]==(int)word[j])
  9.         {
  10.             i++;
  11.             j++;
  12.             flag++;
  13.         }
  14.         else
  15.         {
  16.             i++;
  17.             j=0;
  18.         }
  19.     }
  20. }
  21.  
  22. if(flag==strlen(word))
  23.     cout<<"word found";
  24. else
  25.     cout<<"nt found";
  26.  
A couple of problems in this code
  1. This code assumes that if you are looking for a word of length N then it the word does not occur at sentance[i] it wont occur at sentence[i+1], sentence[i+2] ... sentence[i+N-1]. i.e. It only checks every Nth character of sentence for the search word. This is because you increment the variable i inside the second loop.
  2. If it finds the word then it still carrys on searching the rest of the sentence, most search algorithms return immediately to save processor time. This is because you don't break the outer loop once you have found the word. In fact if the characters of the word exist in the sentence outside the word then flag will get erroneously update resulting in a match fail because it will be > strlen(word)
  3. You perform an unneccessary strlen at the end, you already know if you found the word, no need for this it is a waste of time, you need to set a flag instead of incrementing a flag.
  4. Finally you risk a memory access error, you loop i right up to the end of the sentence but then internall you loop i right up to the end of the word so the maximum size you check is sent[iMax+jMax]. Very possibly outside the range of the array the sent points to. You do not need to check the last characters of sent if there are not enough characters left to fit word into.
  5. This is not an error but on a point of style you have used while loops, however you are looping up the charaters of an array, a for loop would be more common and will make the code look neater.

Problem 1 this is caused by the code fragment

Expand|Select|Wrap|Line Numbers
  1.         if((int)sent[i]==(int)word[j])
  2.         {
  3.             i++;
  4.             j++;
  5.             flag++;
  6.         }
  7.         else
  8.         {
  9.             i++;
  10.             j=0;
  11.         }
  12.  
By incrementing i you prevent a proper search at all the characters of sentence, we need to remove the increment of i. However we still need to loop down sentence by the size of word. This is in fact what j does. Also casts are completely unrequired (assuming sentence and word are both arrays of char or char *). So we re-write this as

Expand|Select|Wrap|Line Numbers
  1.         if( sentence[i+j]==word[j])
  2.         {
  3.             j++;
  4.             flag++;
  5.         }
  6.         else
  7.         {
  8.             j=0;
  9.         }
  10.  
Having done this the j=0; line causes problems, because it will causes the code to start searching from the beiginning of the word again as soon as it finds a non-matching letter. We need to remove it resulting in

Expand|Select|Wrap|Line Numbers
  1.         if( sentence[i+j]==word[j])
  2.         {
  3.             j++;
  4.             flag++;
  5.         }
  6.  


Problem 2 + 3

Caused by the code fragment
Expand|Select|Wrap|Line Numbers
  1.     while(word[j]!='/0')
  2.     {
  3.         if( sentence[i+j]==word[j])
  4.         {
  5.             j++;
  6.             flag++;
  7.         }
  8.     }
  9.  
flag++; is not useful, lets get rid of it we already know it can count erroneous characters giving an incorrect result. Also now we only increment j if we find a letter match. We are seraching the sentence at i for word, as soon as we find a letter mismatch then we stop the loop and go on to sentence[i+1]. If the letters match we need to increment j to test the next letter in the word. If we get to the end of the loop then we have found a match for the word. Also we need to start the search from the beginning of the word each time so j should be initialised to 0 before the loop starts. This code becomes

Expand|Select|Wrap|Line Numbers
  1.     j = 0;
  2.     while(word[j]!='/0')
  3.     {
  4.         if( sentence[i+j] != word[j])
  5.         {
  6.             break;
  7.         }
  8.  
  9.         j++;
  10.     }
  11.  
  12.     if ( word[j] == '\0')
  13.     {
  14.         // word found set flag and break or return
  15.     }
  16.  
Also the strlen test from the end of the code can be removed

Problems 4 and 5
Because you have used while loops the loop control variables are checked and later all over the place substituting with for loops keeps it all together and makes the code neater. Additionally there was nothing to stop the code looking off the end of the array sentence, because the inner loop now always goes to a maximum of strlen(word) and because you don't need to check sentence once you have got to the point where there are not enough characters left to contain word you can stop, this is at the limit strlen(sentence)-strlen(word)

Starting from

Expand|Select|Wrap|Line Numbers
  1. int i=0,j=0,ag=0;
  2.  
  3. while(sentence[i]!='/0')
  4. {
  5.     j = 0;
  6.     while(word[j]!='/0')
  7.     {
  8.         if( sentence[i+j] != word[j])
  9.         {
  10.             break;
  11.         }
  12.  
  13.         j++;
  14.     }
  15.  
  16.     if ( word[j] == '\0')
  17.     {
  18.         // word found set flag and break or return
  19.     }
  20.  
  21.     i++;
  22. }
  23.  
this gives

Expand|Select|Wrap|Line Numbers
  1. int i;
  2. int j;
  3.  
  4. for(i=0; i < strlen(sentence)-strlen(word); i++)
  5. {
  6.     for( j=0; word[j] != '/0'; j++)
  7.     {
  8.         if( sentence[i+j] != word[j])
  9.         {
  10.             break;
  11.         }
  12.     }
  13.  
  14.     if ( word[j] == '\0')
  15.     {
  16.         // word found set flag and break or return found
  17.     }
  18. }
  19.  
  20. // Access flag or return not found here
  21.  
Which is the solution, is less complex and is a couple of lines shorter
Sep 16 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
2
by: amoona | last post by:
Hi… I’m doing a search page form my work for their library. Since this is the first time I work with ASP I’m facing some problems. Each library item has different search data, I was able to...
0
by: Chung Leong | last post by:
Here's a short tutorial on how to the OLE-DB extension to access Windows Indexing Service. Impress your office-mates with a powerful full-text search feature on your intranet. It's easier than you...
5
by: Carstonio | last post by:
I use ASP to display links to Word documents on an intranet. Is there a way in ASP to do text searches on the documents' contents? I want the results to have the link to the Word document plus two...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
2
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
3
by: Moezzie | last post by:
Im going totally crazy over this. Im right now building a verry small solution for searching a mysql-database for up to five keywords. Every file in the database has 2 or more keywords wich the...
5
by: silmana | last post by:
Hi i have this script that i want to use as php or html but i cant find the problem, could anyone solve the problem, i dont know why i cannot use it in php or html file // OBS! Några saker måste...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.