2

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
}
15
  • C does not have a string type. It is all semantics. And a better approach is only to copy the characters you want in the first place, not to copy all then "remove" the unwanted. Also names used by the standard library are reserved. You must not define them yourself. Use my_strlen or any other not colliding name. Note that the function also has a wrong signature. Stick to the standard one, or - better - use the standard function Commented Jan 30, 2016 at 19:17
  • scanf("%c", &c); --> scanf(" %c", &c); note the space before %c which consumes the newline left in the input buffer by the previous scanf. And why did you not #include <string.h> and use the library's strlen? Commented Jan 30, 2016 at 19:18
  • Thanks, sorry, I translated the function names because they weren't in English... Am I wrong with calling the function in line 12? Commented Jan 30, 2016 at 19:20
  • 1
    n = remove(s, c); you can't re-assign an array indentifier. Commented Jan 30, 2016 at 19:22
  • 1
    Just allocate the output string the same length as the input string +1, as you were doing, then read a char from source and if not c, put it in the output, if c, ignore it. Keep doing that until you run out of chars, then shove the terminating null in. Job done. You don't need to copy the input string because you only need to read from it. Commented Jan 30, 2016 at 19:37

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.