removing a substring from a string

This is a discussion on removing a substring from a string within the C Programming forums, part of the General Programming Boards category; Hi, I want to write a function that remove a substring from a string source, and returns. But I want ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    removing a substring from a string

    Hi, I want to write a function that remove a substring from a string source, and returns.
    But I want to do this only using a strlen()function. Can you give me a suggestion how should I make an algorithm for this question?

    e.g.

    Let's say,

    the string source : "abc def abc"
    the string I want to delete: "abc"
    the string source after deleting: "def"
    etc.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The usual method is by typing source code into an IDE and compiling it.

    In other words, this is obviously homework, since you are the second person to ask about it in a day.... so you need to work on it yourself. There is a reason people are assigned homework, and it isn't so they can run to an online forum and beg for help.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Actually, I tried to write something like below. I guess you can help me now?
    Code:
    int strRemoveAll(char src[],char key[]){
          for(i=0;i<strlen(src);i++){
                                     if(src[i]==key[0]){
                                                        for(j=1;j<strlen(key);j++){
                                                                                   if(src[i+j]==key[j])
                                                                                     break;
                                                                                   }
                                                                                   else if (j==(strlen(key)-1))
                                                                                      printf("after the removed string: ", src[i]);
                                                           }

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Why do you use strlen?
    You don't need it.
    Code:
    int strRemoveAll(char *src,char *key)
    {
      while( *src )
      {
        char *k=key,*s=src;
        while( *k && *k==*s ) ++k,++s;
        if( !*k )
        {
          while( *s ) *src++=*s++;
          *src=0;
          return 1;
        }
        ++src;
      }
      return 0;
    }
    
      char s[]="foo bar foo ";
      while( strRemoveAll(s,"foo") );
      puts(s);

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Thanks for your help but,I don't know pointers very well. Therefore, I wanted to use string arrays with strlen().
    I'm sorry that I couldn't understand the code you wrote Could you tell me that if I want to use string arrays with strlen(),
    what should it be?

  6. #6
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by aysek8 View Post
    Thanks for your help but,I don't know pointers very well. Therefore, I wanted to use string arrays with strlen().
    I'm sorry that I couldn't understand the code you wrote Could you tell me that if I want to use string arrays with strlen(),
    what should it be?
    I also don't know pointers very well. I also need an explanation for this

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    If you don't like pointers, i think you should switch to other programming languages.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    If you don't like pointers, i think you should switch to other programming languages.
    He didn't say he doesn't like them... he said he doesn't know them very well...

    Get with your audience, my stupified friend.... the guy's a beginner trying to suss his way through a first time problem.
    We can't ask him to think at our level... and don't you dare chastize him if he doesn't!


    =================

    @OP.... strlen() simply returns the length of a string... use it wherever it's appropriate. Don't let these mugs screw you around.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Substring of a string
    By LotusE in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 03:36 PM
  2. substring in string
    By kenni81 in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:05 PM
  3. how to replace a substring in a string
    By MKashlev in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2002, 07:20 PM
  4. breaking string into substring
    By sadat in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 06:03 AM
  5. seeing if there is a substring in another string
    By canine in forum Windows Programming
    Replies: 2
    Last Post: 12-05-2001, 04:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21