c - 如何使用 C 将字符数组中的两个字符替换为另一个字符?

标签 c arrays loops for-loop while-loop

问题是;我需要将字符串中的“AB”替换为“C”。

示例1:

"HEABLO" to "HECLO"

代码:

    int main() {
    int i=0,j=0;
    char a[]="helablo";
    while(i<strlen(a)-1){
        if(a[i]=='a'&& a[i+1]=='b'){
            a[j]='\0';
            a[j++]='c';
            continue;
        }
        i++;
    }
    if(i==strlen(a)){
        printf("%s\n",a);
    }
    a[j]='\0';
    return 0;
}

没有错误,也没有输出。

最佳答案

您可以编写一个更通用的函数来用单个字符替换子字符串,使用 strstr()查找子字符串,然后 memmove()重写字符串的结尾。

strstr()函数查找输入字符串中子字符串的第一次出现,如果未找到匹配,则返回空指针。该指针可用于分配所需的char到输入字符串。然后memmove()可用于将字符从字符串末尾移动到替换字符后面的位置。如果loc是指向找到的子字符串位置的指针,并且 replace_len是要替换的子串的长度,则 char *tail = loc + replace_len指向应保留的输入字符串的尾部,strlen(tail) + 1是要移动的字符数,包括 '\0'性格。

该函数仅替换第一次出现的子字符串;如果需要替换子字符串的所有出现,可以在循环中调用此函数。自 substr_char()返回指向被替换字符后面的字符的指针(如果没有进行替换,则返回空指针),返回值可用于循环中继续进行替换,直到找不到更多匹配项。

上述方法并不像替换所有出现的子字符串那样有效,因为每次 substr_char() 都会移动字符串的整个尾部,包括要替换的子字符串的剩余实例。叫做。可以修改此方法,以便通过编写单个函数来替换替换子字符串的所有实例,从而不会复制额外的字符。这个功能,substr_char_all() ,找到子字符串的第一个实例,并将替换字符写入 next 指示的位置,然后在 tail 中查找子字符串的下一个实例。如果没有这样的实例,tail移动到替换字符之后。如果存在子字符串的另一个实例,则该实例之前的字符将移动到替换字符之后。通过减去 tail 即可找到要移动的字符数。来自loc ,自 tail指向字符串其余部分的开头,并且 loc指向下一个子字符串实例的开始。请注意,tailloc是指针,因此存储此结果的正确类型是 ptrdiff_t 。指针next然后递增以指示移动字符之后的位置,并且循环继续,将替换字符写入下一个位置,并搜索替换子字符串的更多实例。

#include <stdio.h>
#include <string.h>
#include <stddef.h>           // for ptrdiff_t, used in substr_char_all()

char * substr_char(char *str, char *substr, char c);
void substr_char_all(char *st, char *subst, char c);

int main(void)
{
    /* Replace the first occurrence of a substring with a character */
    char string1[] = "heablo";
    printf("Before substitution: %s\n", string1);
    char *found = substr_char(string1, "ab", 'l');
    printf("After substitution: %s\n", string1);

    putchar('\n');

    /* Replace all occurrences of a substring with a character */
    char string2[] = "heababababloo";
    printf("Before substitution: %s\n", string2);
    found = string2;
    while ((found = substr_char(found, "ab", 'l'))) {
        continue;
    }
    printf("After substitution: %s\n", string2);

    /* Using the substr_char_all() function */
    putchar('\n');
    char string3[] = "heablo";
    printf("Before substitution: %s\n", string3);
    substr_char_all(string3, "ab", 'l');
    printf("After substitution: %s\n", string3);

    putchar('\n');
    char string4[] = "heababababloo";
    printf("Before substitution: %s\n", string4);
    substr_char_all(string4, "ab", 'l');
    printf("After substitution: %s\n", string4);

    putchar('\n');
    char string5[] = "some body wants some thing";
    printf("Before substitution: %s\n", string5);
    substr_char_all(string5, "some", 'a');
    printf("After substitution: %s\n", string5);

    return 0;
}

/* Returns a pointer to the character following substitution, or NULL */
char * substr_char(char *st, char *subst, char c)
{
    char *loc = strstr(st, subst);
    if (loc) {
        *loc = c;
        char *tail = loc + strlen(subst);
        memmove(loc + 1, tail, strlen(tail) + 1);
        ++loc;
    }

    return loc;
}

void substr_char_all(char *st, char *subst, char c)
{
    char *loc = strstr(st, subst);
    char *next = loc;
    while (loc) {
        *next++ = c;
        char *tail = loc + strlen(subst);
        if ((loc = strstr(tail, subst))) {
            ptrdiff_t copy_len = loc - tail;
            memmove(next, tail, copy_len);
            next += copy_len;
        } else {
            memmove(next, tail, strlen(tail) + 1);
        }
    }
}

程序输出:

Before substitution: heablo
After substitution: hello

Before substitution: heababababloo
After substitution: hellllloo

Before substitution: heablo
After substitution: hello

Before substitution: heababababloo
After substitution: hellllloo

Before substitution: some body wants some thing
After substitution: a body wants a thing

关于c - 如何使用 C 将字符数组中的两个字符替换为另一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45464018/

相关文章:

python - 从多维列表python中删除不必要的层

javascript - 谁或哪个线程拥有 Javascript 事件循环?

java - 如何编写一个简单的 Java 程序来找到两个数之间的最大公约数?

c - ARM Cortex-M4 : Running code from external flash

c - 将 unsigned short int 初始化为有符号值

C 气泡问题

arrays - Numpy 数组内存管理

java - 打印数组和 toString 时的输出问题

c - 将数组分配给* mut c_void

c++ - 如何知道所使用的微软 C runtime 的版本?