Search code examples
cstringmemmove

Removing characters from an input string in c


My objective is to remove user defined amount of characters from a string in C.

The code requires the user to input a string, indicate the start of the characters they want removed and indicate how many characters from that position they want removed & then the code displays the result.

I'm hoping someone out there can come up with a code that does the required function and with step-by-step info because I only started coding yesterday

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


 int a,b;
 char text[20];
 char new_sentence[20];
 int how_much;

 void removeString(char* text, int b, int how_much);
 int main(void)

 {


  printf("\nEnter your sentence: ");
  gets(text);
  printf("\nWhere to start: ");
  scanf("%d",&a);
  printf("\nHow many characters do you want to remove: ");
  scanf("%d",&how_much);

  removeString(char* text, int b, int how_much);

  printf("\nNew sentence: %s",text);

  return 0;
  }


  void removeString(char* text, int b, int how_much)
   {
     how_much = b - a;

     memmove(&text[a], &text[b],how_much);

     }

Solution

  • I will not write the code for you, as the example code you've provided is indeed a minimal example but these are the things you should to:

    1. Change removeString() function signature (and its body accordingly) to removeString(char* your_string, int where_to_start, int how_much_to_cut). Note that in C char[] is equivalent to char* and in your case it is better to pass a char* to your function, as you will have variable-length strings.
    2. Use strlen() function to calculate length of the string (i.e. char*) passed to removeString() function. You can error-check parameters based on length calculated that way.
    3. Note that you may have a case in which you will have to cut some characters in the middle of the string - therefore, I would calculate the length of the resulting string and either realloc() the char* your_string or malloc() new string and return it from removeString() function.
    4. You can add input parameters (string, where to cut, how much characters to cut) to your program by following information on this webpage.

    EDIT:

    You can see solution to your problem below:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_LEN 20
    
    char* removeString(char* text, int where_to_start, int how_much)
    {
        // You should consider all the special (invalid) cases here
        // I'm demonstrating how to handle common case
        size_t len = strlen(text);
        char* new_string = (char*) malloc(sizeof(char) * len - how_much);
        memcpy(new_string, text, where_to_start);
        memcpy(new_string+where_to_start, text+where_to_start+how_much, len-where_to_start-how_much);
        return new_string;
    }
    
    int main(void)
    {
        int a,b;
        char buffer[MAX_LEN];
    
        printf("Enter your sentence:\n");
        scanf("%s", buffer);
    
        printf("Where to start:\n");
        scanf("%d",&a);
        printf("How many characters do you want to remove:\n");
    
        scanf("%d",&b);
    
        char* removed = removeString(buffer, a, b);
    
        printf("New sentence: %s\n", removed);
    
        free(removed);
        return 0;
    }