0

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

     }

1 Answers1

0

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;
}
syntagma
  • 23,346
  • 16
  • 78
  • 134
  • I am aware that I have provided a minimal example so I edited the post to show what I came up with in the last few minutes. Any specific alterations would be welcome while I attempt to use the instructions you have given – Nathan Piper May 04 '15 at 17:40
  • Looks fine to me, try to pass parameters to `removeString()` now. – syntagma May 04 '15 at 17:41
  • it worked. Unfortunately, all I did was remove the & from [&a] and [&b] in the memove function so I didn't even begin to use your solution but I'll be glad to accept your answer if you tell me how to change the third argument to 'how much they want cut' rather than 'the final character to cut' – Nathan Piper May 04 '15 at 17:51
  • I see you have added the final character as input parameter. I still think you should add some input validation and take into account special case I'm mentioning. As for your question, why not just `how_much = final_char_pos - start_char_pos`? – syntagma May 04 '15 at 17:56
  • not working. how_much = text[b] - text[a] I assume. I've made an integer variable for how_much I've changed the scanf function to give me how_much and included the arithmetic in the removestring function. Sorry but you would need to be absolutely clear with me because I don't know what I'm doing – Nathan Piper May 04 '15 at 18:18
  • Assuming you're getting numbers indexed from 0 (as C arrays are), you can just do: b-a. – syntagma May 04 '15 at 18:26
  • console crashed. when attempting to put the number of characters to remove – Nathan Piper May 04 '15 at 19:03
  • thanks for the help but the console crashes for any string more than three characters & while trying to fix it, I got confused with a few processes you've carried out – Nathan Piper May 04 '15 at 22:47
  • Again, it's not finished but the most common use case is covered and I think you should know how to go from there. Also, I have tried it with string more than 3 characters, maybe you are entering numbers that cause error cases to happen. – syntagma May 04 '15 at 22:49