1

Overall goal statement :

Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters.

I have two arrays of the same size. if I want to remove the first and last element in the first array and add them to next array. suppose that I can't change the size of second array to (sizeof(firstarray) - 2) in the initial declaration;

if first array(string) is -

 char* array1[] = {"eloquent"};

and then the second array should be -

char* array2[] = {"loquen"};

I tried to do it and it does not work in some cases.

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

char* remove_char(char* dst, const char* src)
{
  free(dst);
  dst = malloc(((int)sizeof(src) - 2));
  for (int i = 1; i < ((int)sizeof(src) - 1); i++)
    {
      dst[i - 1] = src[i];
  }
  /* src is the input string */
  /* your solution should write the result into dst and return it */
  return dst;
}
5
  • whats the question?
    – UpAndAdam
    Commented Jan 11 at 18:07
  • "Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters." Commented Jan 12 at 0:27
  • 1
    in your inital post you didnt post an attempt. so it was unclear what you were stuck on other than not knowing how to do it.
    – UpAndAdam
    Commented Jan 12 at 15:08
  • 1
    I will check for follow ups to help improve my answer... this while seemingly simple question is actually deeper than it seems and is a REALLY good question to ask. I have upvoted accordingly.
    – UpAndAdam
    Commented Jan 12 at 15:48
  • what do you mean by add them to next array?
    – UpAndAdam
    Commented Jan 12 at 16:03

1 Answer 1

1

You are on the right track but you have a few problems.

  • Do not free the pointer dst if you use it as an input parameter. You have to assume it's already been free'd or doesn't need to be or you will potentially introduce double free memory errors.

This assumes you can change the function signature

  • Don't pass dst in at all only pass in the src, return dst.

  • since you don't have to worry about strings with less than two characters if strlen(src) < 2 || sizeof(src) < 2 you can just return 0 (null) right away.

  • while sizeof(src) would give you the size of the array you are forgetting that that size includes 1 or more null terminators*. Because of the nature of C you will need to employ both sizeof and strlen here. If strlen >= sizeof it isn't null terminated and this is a problem but not one I think they are going to force on you. In your main case strlen is how many characters you need to focus on in the loop and you will have to put the null terminator in manually. So you should malloc strlen(src)+1-2 as your size, +1 for null, -2 for removing two chars.

  • your iteration range for the copying should then be over strlen-1 instead of size-1

  • finally you should put dst[strlen(src)] = 0 to put the null terminator in place.

  • note that technically they could give you 0 null terminators but for now I'm going to assume they arent that cruel because it would greatly complicate your logic. You should confirm what is fair game. Also note that they could give you multiple null terminators... your string input could be "foobar\0" which has 2 NULL's (i.e. char src[]="foobar\0"; where as char src[]="foobar" has 1 null, while char src[6]="foobar" has none)
4
  • Thank you very much for your help! looks like you help many people~ I really appreciate that again thank you Commented Jan 14 at 4:05
  • I understood that I confused in the case of strlen and sizeof. thank you for poinitng that. and that dst[strlen] is really usefull cause I havent seen it in any tutorial! Commented Jan 14 at 4:15
  • 1
    sorry to be clear that should be dst[strlen(src)] = 0;
    – UpAndAdam
    Commented Jan 15 at 3:49
  • 1
    that's what I do here, I help people. it's good exercise for me to constantly review others code.
    – UpAndAdam
    Commented Jan 15 at 3:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .