简体   繁体   English

使用动态字符串从C中的字符串中删除字符

[英]Removing a character from string in C with a dynamic string

So, I want to create a function which creates and returns a dynamic string based on a string s without characters c . 因此,我想创建一个函数,该函数基于不带字符c的字符串s 创建并返回动态字符串。 Now, I want to be able to remove all of the desired characters, no matter the case. 现在,无论哪种情况,我都希望能够删除所有所需的字符。 Additionally, the original string entered by the user should remain unchanged. 此外,用户输入的原始字符串应保持不变。 Here's my attempt, it keeps telling me about an error at line 12 (noted in the comments). 这是我的尝试,它总是在第12行告诉我有关错误的信息(注释中已指出)。

One more thing: I'm not sure if I wrote the remove function well, I think it should work? 还有一件事:我不确定我是否很好地编写了remove函数,我认为它应该可以工作吗? All of the pointers confused me a little bit. 所有的指针使我有些困惑。

#include <stdio.h>
#include <stdlib.h>
char * remove(char *s, char c);
int strlen(char *s);

int main() {
    char s[16], c, n[16];
    printf("Please enter string: ");
    scanf("%s", s);
    printf("Which character do you want to remove? ");
    scanf("%c", &c);
    n = remove(s, c);  // Place the new string in n so I wouldn't change s (the error)
    printf("The new string is %s", n);
    return 0;
}
int strlen(char *s)
{
   int d;
   for (d = 0; s[d]; d++);
   return d;
}

char * remove(char *s, char c) {
    char str[16], c1;
    int i;
    int d = strlen(s);
    str = (char)calloc(d*sizeof(char)+1);
    // copying s into str so I wouldn't change s, the function returns str
    for (i = 0; i < d; i++) { 
        while(*s++ = str++);
    }
    // if a char in the user's string is different than c, place it into str
    for (i = 0; i < d; i++) {
        if (*(s+i) != c) {
            c1 = *(s+i);
            str[i] = c1;
        }
    }
    return str;   // the function returns a new string str without the char c
}

You declared n as 16-element array of char type: 您将n声明为char类型的16元素数组:

char n[16];

So you cannot do: 因此,您无法执行以下操作:

n = remove(s, c);

because n is a const pointer. 因为n是const指针。

Also your remove function returns a pointer to its local array, which gets destroyed as soon as your function returns. 同样,您的remove函数将返回一个指向其本地数组的指针,该指针将在函数返回后立即销毁。 Better declare remove as 更好的声明remove

void remove(char *to, char *from, char var);

and pass n as the first parameter. 并传递n作为第一个参数。

There ware so many mistakes in your program it was easier to rewrite and show you, with added comments. 在您的程序中发现了如此多的错误,因此添加注释后,更容易重写和显示。 Note that scanf("%s... will accept only a single word, not a sentence (it stops at the first whitespace). And note that the newline will be left in the input buffer for scanf("%c... to read unless you add a space, as advised. 请注意, scanf("%s...将只接受一个单词,而不是一个句子(它停在第一个空白处)。并且请注意, newline将留在scanf("%c...的输入缓冲区中除非另有说明,否则请阅读。

#include <stdio.h>

void c_remove(char *n, char *s, char c) {   // renamed because remove() is predefined
    while (*s) {                            // no need for strlen()
        if (*s != c)                        // test if char is to be removed
            *n++ = *s;                      // copy if not
        s++;                                // advance source pointer
    }
    *n = '\0';                              // terminate new string
}

int main(void) {                            // correct signature
    char s[16], c, n[16];
    printf("Please enter string: ");
    scanf("%s", s);
    printf("Which character do you want to remove? ");
    scanf(" %c", &c);                       // the space before %c cleans off whitespace
    c_remove(n, s, c);                      // pass target string pointer too
    printf("The new string is %s", n);
    return 0;
}

Program sessions: 计划会议:

Please enter string: onetwothree
Which character do you want to remove? e
The new string is ontwothr

Please enter string: onetwothree
Which character do you want to remove? o
The new string is netwthree

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

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