繁体   English   中英

c - 如何从字符串的开头和结尾删除特殊字符

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

假设我有一个像 $$$hello$$$ 这样的字符串,如何从开头和结尾删除 $$$ 以使其看起来像 hello。

另一个例子是 $$he$$o$$ 之后是 he$$o

这是我的替代解决方案:

#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 );
}

上面的解决方案将子字符串复制到另一个缓冲区。 如果要将子字符串复制到源字符串的开头,可以使用函数memmove而不是memcpy 有必要改用这个函数,因为memcpy不允许源缓冲区和目标缓冲区重叠,而memmove允许。

使用这种技术将子字符串复制到源字符串的开头,我提出了以下解决方案:

#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. 记住第一次出现的字母,这是beg指针
  2. 在每个字母后放置end指针
  3. begend打印结果
#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);
}

您还可以通过将您想要的旧字符串的内容复制到新字符串中来分配内存。

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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