简体   繁体   中英

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 . 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).

One more thing: I'm not sure if I wrote the remove function well, I think it should work? 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:

char n[16];

So you cannot do:

n = remove(s, c);

because n is a const pointer.

Also your remove function returns a pointer to its local array, which gets destroyed as soon as your function returns. Better declare remove as

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

and pass n as the first parameter.

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.

#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

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