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

Delete a word from a string

Delete the words that mathes to the key. It's not my homework i learn "c" by myself.

I wrote this
Expand|Select|Wrap|Line Numbers
  1. int wdel(char* a, char* key){
  2. // (a) is the string and (key) is the word
  3. if(!a || !key) return 0;
  4. int i,k=0,l,n;
  5. char *p;
  6. n=lens(key); //(n) is the lenght of the word i want to delete 
  7.     for(i=0;a[i]!=0;i++){
  8.     if(a[i]==key[k]) { l++; k++; }
  9.     if(a[i]==' ') { l=0; k=0; }
  10.           if(l==n){
  11.           p=&a[i];  
  12.           strcpy((p-n)+1,p+1);
  13.           }
  14.     }
  15. return 1;
  16. }
It deletes the word only when it has n+2-spaces after it.
Ex: I was"+5 spaces" very happy. in this case it will delete the word "was" correctly.
Apr 25 '10 #1
4 10112
jkmyoung
2,057 Expert 2GB
!b is !key?

I'm a little confused to your method.
You find where the first string matches the first character in key.
but then what do you do with l?

if n > 1 then I don't see how this algorithm can possibly work. You need to start a nested loop inside the for loop in order to compare characters.
Apr 27 '10 #2
@jkmyoung
i found the error, now it works
Expand|Select|Wrap|Line Numbers
  1. int wdel(char* a, char* key){
  2. if(!a) return 0;
  3. int i,k=0,l=0,n;
  4. char *p;
  5. n=lens(key);
  6.     for(i=0;a[i]!=0;i++){
  7.         if(a[i]==key[k]) { l++; k++; }
  8.         if(l==n){
  9.         p=&a[i];  
  10.         strcpy((p-n)+1,p+1);
  11.         l=0; k=0;
  12.         }
  13.     }
  14. return 1;
  15. }
n is the lenght of the key; 'l' is the number of letters that match to exclude the case when i have the word 1) "work" and 2)"working".
in the first case l==n and it would delete "work", and in the second l<n and working wouldn't be deleted
Apr 27 '10 #3
jkmyoung
2,057 Expert 2GB
Try matching:
("this text of string tttt over" , "tttt")

I think you might be surprised at the results.
Apr 27 '10 #4
@jkmyoung
Expand|Select|Wrap|Line Numbers
  1. /*Try matching:
  2. ("this text of string tttt over" , "tttt")
  3. I think you might be surprised at the results. 
  4. */
the result is "this test of string over";
the second function works after i deleted the second if in the loop.

my problem is solved, can delete now this topic, but if someone would show me another way how to solve my problem (it would be great). only some basic ideas
Apr 27 '10 #5

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

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
12
by: deko | last post by:
Is there a quick and dirty way to delete all files in a given directory? perhaps something like this: Set objFile = CreateObject("Scripting.FileSystemObject") objFile.DeleteFile "all files in...
3
by: Jim Bob | last post by:
I have a character Data field coming in such as 06-1460-2 40 5000 55 5 50-4-55-6445 I need to strip the dash, blanks or any other non numeric character from this field and return just the...
13
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything...
13
by: Shelley | last post by:
Compare Current Year Worksheet with Previous Year Worksheet and if SSN exists in Current Year Worksheet & Not in Previous Year - Copy this Row from Current Year Worksheet & Paste into Previous Year...
15
by: buddhatown | last post by:
Very simple question for all you folks out there. I am total noob with js. I have a list called drawPathList thats just a list of xy coordinates. I use this to construct a drawing on a map. ...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
2
by: Smurfas | last post by:
Hi, I have another problem. I char converter to string = variable; , and now string text end is empty place. I must new string(variable) plus word: "end"; Very simple: a = variable + "end"; But...
2
by: felipebucaram | last post by:
Hi, I'm using word an visual basic I'm makin an userform to paste an delete text. My question is: how do I delete a string? or a an entire line? Example: 1) Passport delievred in 13/56/1978....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.