c - C 字符串中的子字符串替换

标签 c string substring

大家好!我决定在这个网站上注册一个帐户来寻求帮助,因为我找不到任何可以帮助我的东西。我有一个需要多个步骤的问题: 1)从键盘读取一个字符串; 2) 读取一个整数,代表要修改的子字符串的个数(修改就是将字符串中的所有字符都转换为“*”); 3)读取所有需要修改的子串; 4) 打印修改后的初始字符串。 我尝试通过首先设法用一个子字符串来完成它,但是当我读取多个子字符串时我不知道该怎么做。所以,这是版本 版本1

#include<stdlib.h>
#include<string.h>
int main()
{   
    int n;
    char str[5120];
    char substr[100];
    printf("Type the string:\n");
    gets(str);
    printf("\nType the substring which you would like to modify:");
    gets(substr);
    char *p;
    p = strstr(str,substr);
        if (*p != '\0')
            {
                for( int j=0 ; j< strlen(substr); j++)
                        {
                            *p = '*';
                            p++;
                        }
                }
    printf("\nThe modified string is:\n");
    for ( int i=0 ; i< strlen(a) ; i++)
            {
                printf("%c",a[i]);
            }
    return 0;
}

以及我尝试修改多个的版本

#include<stdlib.h>
#include<string.h>
int main()
{   
    int n;
    char str[5129];
    char substr[n][100];
    printf("Type the string:\n");
    gets(str);
    printf("How many substrings to modify?");
    scanf("%d",&n);
    printf("\nType the substrings:");
    for( int i=0 ; i< n ; i++)
        {
            scanf("%s",substr[i]);
            printf(" ");
        }
    for ( int i=0; i < n; i++)
        {
            char *p;
            p = strstr(str,substr[i]);
            if (*p != '\0')
                {
                    for( int j=0 ; j< strlen(cuv[i]); j++)
                        {
                            *p = '*';
                            p++;
                        }
                }
        }
    printf("\nThe modified string is:\n");
    for ( int i=0 ; i< strlen(str) ; i++)
            {
                printf("%c",str[i]);
            }
    return 0;
}```
I guess for the n times that I cycle to the string I have to allocate memory somewhere, or I don't know..
Please, I need some help! Thank you!

最佳答案

所以我根据你和@PaulOgilvie的建议编辑了我的答案。我希望该程序现在能够按您的预期运行。

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

#define MAX_SIZE 5129

void replaceSubstring(char *p, int len){
    for(int i = 0; i < len; i++, p++){
        *p = '*';
        }
    }

int main() {   
    int n = 0;
    char str[MAX_SIZE];
    char substr[n][100];

    printf("Type the string:\n");
    fgets(str, MAX_SIZE, stdin);

    printf("How many substrings to modify?");
    scanf("%d",&n);


    for( int i=0 ; i< n ; i++) {
        printf("\nType the substring number %d:", i+1);
            scanf("%s",substr[i]);
    }

    char *p;
    for ( int i=0; i < n; i++){
         p = strstr(str, substr[i]);
         while(p != NULL) {
             replaceSubstring(p, strlen(substr[i]));
             p = strstr(str, substr[i]);
             }
    }

    printf("\nThe modified string is:\n");
    for ( int i=0 ; i< strlen(str) ; i++)
            {
                printf("%c",str[i]);
            }
    return 0;
}

关于c - C 字符串中的子字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59531546/

相关文章:

java - 如何在字符串中查找非特定子字符串?

c - C 中的 "-1L"/"1L"是什么?

c - 如何使用 GSL 将任意函数(即 1/x + 1/x^2)拟合到某些数据?

c - Shell 排序中的 h 排序

c - 没有 malloc 或 calloc 的 free() 函数

.net - 如何连接 ArrayList 的元素将其转换为字符串表示形式?

java - 正则表达式 - 替换文本,忽略圆括号中的文本

javascript - 如何在第一个 `/` (斜杠)处拆分字符串并将其部分包围在 `<span>` 中?

c++ - 如何在 C++ 中实现通用哈希函数

mysql - 将 INNER-JOIN 与 UNION ALL 和 mysql 中的另一个表一起使用