0

I've seen a lot of programs that trims leading and trailing whitespaces using C. Now, Im testing my knowledge in pointers. I want to create my own programs that trims whitespaces. I have no issue creating the part that trims theleading whitespaces. My question now is why does the code that I create for removing the trailing whitespaces not working ? Please take note of the question, why.

char * str = calloc(2000,sizeof(1));
char * modStr = calloc(2000,sizeof(1));
int start, i, end;
strcpy(str,"    test      ");

int len = strlen(str);

i=0;
while(isspace((int) *str)!=0){
    if(i>=len)
        break;  
    printf("%d\n",i);
    i++;
    str++;
}
printf("first str:%s",str);
start = i;

i = strlen(str);

strcpy(modStr, str);
i=strlen(modStr);
--modStr;
while(isspace( *modStr)!=0){
    if(i==0)
        break;  
    printf("%d:%c\n",i,*modStr);
    i--;
    --modStr;
}

*modStr = 0;

I was able to remove the trailing whitespaces but when I try to print the string, it is empty. Could you tell me what's wrong?

6
  • I'm a bit lost. It looks like you're making a copy of the string to trim the end, but you're not correctly setting the working pointer to the end of that: you're leaving it the start then walking backwards. You're also then trying to truncate the copied string not the original. Commented Jun 6, 2014 at 10:31
  • 1
    Also this may not be what you mean: char * str = calloc(2000,sizeof(1)); I think this is creating an 8000 character buffer, not 2000. You should also take care not to let the user overflow this for you, e.g. by restricting the length of the strcpy. Commented Jun 6, 2014 at 10:33
  • 1
    As a sidenote, I don't recommend modifying original pointers which get the address from calloc(). You cannot free() those later, unless you keep the original start addresses. Commented Jun 6, 2014 at 10:34
  • @Rup does changing it to char * str = calloc(2000,sizeof(char)); corrects the code? and regarding strcpy, so what you're trying to say is make use of strncpy? am I right? Commented Jun 8, 2014 at 11:59
  • @user694733 what do you suggest I use? malloc or array? thank you! Commented Jun 8, 2014 at 12:02

2 Answers 2

2

Your modStr is pointing to the beginning of the string and your code supposes it points to the end. Instead of:

strcpy(modStr, str);
i=strlen(modStr);
--modStr;

try something like:

strcpy(modStr, str);
modStrBegin = modStr;
i=strlen(modStrBegin);
modStr = modStrBegin + i - 1;

you will need to add definition char *modStrBegin; at the beginning of your code.

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

3 Comments

The original code is even doing --modStr so it's pointing to a position before the start of the string.
@Marian okay got it. Lastly, modStrBegin is a char*. I know this line modStr = modStrBegin + i - 1; is used to point to the last char of modStr. But why add modStrBegin? why is this not possible modStr = i + i - 1; ?
i is the size of the string i-1 is the index of the last character. However, you need to get address of the end of string, i.e. the place in memory where the last character is stored. So for example if the string is stored at the address 1000000 (so this is the address of its first character), then the address of the last character will be 1000000+i-1.
0

Your version is a bit too clumsy and unnecessarily complicated.

The simplest version of a strip string functionality I could come up with is the following:

struct Str
{
    char const *beg, *end;
};

Str strip_string(char const* beg) {
    Str r;
    // Skip the leading whitespace.
    while(*beg && isspace(*beg))
        ++beg;

    // Find the last non-whitespace character in the string.
    r.beg = r.end = beg;
    while(*beg)
        if(!isspace(*beg++))
            r.end = beg;

    return r;
}

Note that the above function just finds the begging and ending of the non-whitespace sequences in the string. You may like to duplicate the resulting Str if necessary,

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.