简体   繁体   中英

how to remove special character from the beginning and end of a string in c

Lets say I have a string like $$$hello$$$ how can I remove $$$ from the beginning and the end to make it look like hello.

another example is $$he$$o$$ after would be he$$o

Here is my alternate solution:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int main( void )
{
    char source_string[] = "$$he$$o$$";
    char *p1, *p2;

    // find start of substring to extract
    p1 = source_string;
    while ( *p1 == '$' ) p1++;

    //find start of "trash" at end
    p2 = &source_string[ strlen(source_string) ];
    while ( p2 != p1 && *(p2-1) == '$' ) p2--;

    //calculate length of substring to extract
    size_t len = p2 - p1;

    //allocate space for dest_string including null terminating character
    char *dest_string = malloc( len + 1 );
    assert( dest_string != NULL );

    //extract substring
    memcpy( dest_string, p1, len );

    //add null terminating character
    dest_string[len] = '\0';

    //print the result
    puts( dest_string );

    //free the allocated buffer
    free( dest_string );
}

The solution above copies the substring to another buffer. If you want to copy the substring to the start of the source string instead, you can use the function memmove instead of memcpy . It is necessary to use this function instead, because memcpy does not allow the source and destination buffers to overlap, whereas memmove does.

Using this technique to copy the substring to the start of the source string, I propose the following solution:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int main( void )
{
    char str[] = "$$he$$o$$";
    char *p1, *p2;

    // find start of substring to extract
    p1 = str;
    while ( *p1 == '$' ) p1++;

    //find start of "trash" at end
    p2 = &str[ strlen(str) ];
    while ( p2 != p1 && *(p2-1) == '$' ) p2--;

    //calculate length of substring to extract
    size_t len = p2 - p1;

    //copy substring to start of string
    memmove( str, p1, len );

    //add null terminating character
    str[len] = '\0';

    //print the result
    puts( str );
}
  1. Remember first occurance of a letter, this is beg pointer
  2. Place end pointer after every letter
  3. Print result from beg to end
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  char input_str[] = "$$$h&e$$l&&lo$$$";
  char* beg = NULL, *end = NULL;
  int i;
  for (i = 0; i < sizeof(input_str); ++i) {
    if (input_str[i] >= 'a' && input_str[i] <= 'z') {
      end = &input_str[i+1];
      if (beg == NULL)
        beg = &input_str[i];
    }
  }
  if (!beg || !end)
    return 0;
  *end = 0;
  printf("%s\n", beg);
}

You could also allocate memory by copying the contents of the old string you want into a new string.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *trim_word(char *word, char *to_remove)
{
    int beg_pos = 0;
    int end_pos = strlen(word);
    // at the end of the while loop, beg_pos represents the first position
    // in the string that is not the char to remove
    while (beg_pos < end_pos && word[beg_pos] == to_remove[0])
        beg_pos++;
    // at the end of the while loop, end_pos is the last position in string
    // that is not the char to remove
    while (end_pos - 1 > 0 && word[end_pos - 1] == to_remove[0])
        end_pos--;
    char *new_word = NULL;
    // allocate end_pos - beg_pos + 1
    new_word = malloc(end_pos - beg_pos + 1);
    if (new_word == NULL) {
        fprintf(stderr, "Error malloc.\n");
        exit(1);
    }
    // copy in new_word starting at word[beg_pos] for (end_pos - beg_pos) characters
    strncpy(new_word, &word[beg_pos], end_pos - beg_pos);
    new_word[end_pos - beg_pos] = '\0';
    return new_word;
}

int main(void)
{
    char *str = "$$$he$$o$$$";
    char to_remove = '$';
    char *trimmed_word = NULL;
    trimmed_word = trim_word(str, &to_remove);
    printf("old word: %s\t trimmed word: %s\n", str, trimmed_word);
    // free allocated memory in trimmed_word function
    free(trimmed_word);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM