Delete a word from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waaadim
    New Member
    • Apr 2010
    • 3

    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
    Code:
    int wdel(char* a, char* key){
    // (a) is the string and (key) is the word
    if(!a || !key) return 0;
    int i,k=0,l,n;
    char *p;
    n=lens(key); //(n) is the lenght of the word i want to delete 
    	for(i=0;a[i]!=0;i++){
    	if(a[i]==key[k]) { l++; k++; }
    	if(a[i]==' ') { l=0; k=0; }
    	      if(l==n){
    	      p=&a[i];  
    	      strcpy((p-n)+1,p+1);
    	      }
    	}
    return 1;
    }
    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.
    Last edited by Frinavale; Apr 27 '10, 09:35 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    !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.

    Comment

    • waaadim
      New Member
      • Apr 2010
      • 3

      #3
      Originally posted by jkmyoung
      !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.
      i found the error, now it works
      Code:
      int wdel(char* a, char* key){
      if(!a) return 0;
      int i,k=0,l=0,n;
      char *p;
      n=lens(key);
      	for(i=0;a[i]!=0;i++){
      		if(a[i]==key[k]) { l++; k++; }
      		if(l==n){
      		p=&a[i];  
      		strcpy((p-n)+1,p+1);
      		l=0; k=0;
      		}
      	}
      return 1;
      }
      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
      Last edited by Frinavale; Apr 27 '10, 09:36 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Try matching:
        ("this text of string tttt over" , "tttt")

        I think you might be surprised at the results.

        Comment

        • waaadim
          New Member
          • Apr 2010
          • 3

          #5
          Originally posted by jkmyoung
          Try matching:
          ("this text of string tttt over" , "tttt")

          I think you might be surprised at the results.
          Code:
          /*Try matching:
          ("this text of string tttt over" , "tttt")
          I think you might be surprised at the results. 
          */
          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
          Last edited by Frinavale; Apr 27 '10, 09:36 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

          Comment

          Working...