Thread: A method to deleted a substring from a string

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    2

    A method to deleted a substring from a string

    Hello, I am trying to delete a substring from a string. I am trying to use pointers and return the value where the substring occurrence is found. But I am lost. Here is what I have so far ----

    Code:
    int delete_substring(char *str, char *substr)
    {
      int i;
      char *position = strstr(str, substr);
      if (!position)
        return -1;
      return position - str;
    }
    Last edited by Salem; 10-21-2022 at 11:08 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    You seem to have managed the slightly harder insert substring in your other post.
    Creating insert substring method?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would suggest writing an function to find the substring.

    After you get that done then trying to write an delete substring.

    Trying to write one function that does both is not an normal thing to do.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,737
    Quote Originally Posted by idiotKid View Post
    Hello, I am trying to delete a substring from a string. I am trying to use pointers and return the value where the substring occurrence is found. But I am lost. Here is what I have so far ----

    Code:
    int delete_substring(char *str, char *substr)
    {
      int i;
      char *position = strstr(str, substr);
      if (!position)
        return -1;
      return position - str;
    }
    While it's possible to remove directly from the string it's significantly more difficult than say:
    Code:
    char *cpy = calloc( strlen(str) + 1, 1 );
    char *pos = strstr( str, txt );
    int len = (int)(pos - str);
    if ( !cpy || !pos )
    	return NULL;
    sprintf( cpy, "%.*s%s", len, str, pos + strlen(txt) );
    return cpy;
    Trust me on that point, I actually wrote those type of functions for the library I'm making, doubt they're as fast as the above though for what you want

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    Maybe a better routine:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *strstrdel( char *str, const char *substr )
    {
      char *p = strstr( str, substr );
    
      if ( p )
      {
        char *q = p + strlen( substr );
        memmove( p, q, strlen ( q ) + 1 );
      }
    
      return str;
    }
    
    int main( void )
    {
      char strs[][32] = { "abcdef", "defabc", "xabcyz", "" };
      int i;
    
      for ( i = 0; i < 4; i++ )
      {
        printf( "\"%s\" -> ", strs[i] );
        printf( "\"%s\"\n", strstrdel( strs[i], "abc" ) );
      }
    }

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,737
    Quote Originally Posted by flp1969 View Post
    Maybe a better routine:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *strstrdel( char *str, const char *substr )
    {
      char *p = strstr( str, substr );
    
      if ( p )
      {
        char *q = p + strlen( substr );
        memmove( p, q, strlen ( q ) + 1 );
      }
    
      return str;
    }
    
    int main( void )
    {
      char strs[][32] = { "abcdef", "defabc", "xabcyz", "" };
      int i;
    
      for ( i = 0; i < 4; i++ )
      {
        printf( "\"%s\" -> ", strs[i] );
        printf( "\"%s\"\n", strstrdel( strs[i], "abc" ) );
      }
    }
    That certainly works too, definitely faster than allocating memory, though if you want to replace the sub string then my method is easier to adapt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating insert substring method?
    By glasgow in forum C Programming
    Replies: 1
    Last Post: 10-21-2022, 06:44 PM
  2. How to enter new value to a deleted string
    By sumanthyj in forum C Programming
    Replies: 1
    Last Post: 02-19-2019, 10:39 PM
  3. Substring of a string
    By LotusE in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 03:36 PM
  4. substring in string
    By kenni81 in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:05 PM
  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

Tags for this Thread