2
char* clean_string (char *input_string){
  /*Ensure that input string isn't null and only do heavy lifting if it's not null*/
  if (input_string){
    char *stripped;
    stripped = (char*)malloc(strlen(input_string)*sizeof(char));
    while (*input_string != '\0'){
      if isalpha(*input_string){
        *stripped = toupper(*input_string);
    input_string++;
    stripped++;
      } else {
        input_string++;
    }
  }
/*       *stripped++ += '\0';*/
   return stripped;
  }
 /*default return val*/  
return NULL;
}

Can anybody tell me where I'm going wrong with this? Tried to do a test run and it doesn't output anything when I try to call it.

4
  • What do you mean by "output"?
    – littleadv
    Commented Sep 15, 2011 at 7:04
  • store the initial value of the pointer stripped and return that pointer and uncomment the part *stripped++ += '\0'; Commented Sep 15, 2011 at 7:07
  • Try to post compilable code: if isalpha(*input_string){ does not compile! Commented Sep 15, 2011 at 7:13
  • Got it fixed and working thanks to everyone's input.
    – diz
    Commented Sep 15, 2011 at 16:13

4 Answers 4

7

You are returning a pointer to the last character in the string (stripped++ ?).
You are allocating one byte too few (should be strlen(...) + 1).

stripped = (char*)malloc(strlen(input_string)*sizeof(char)); /* Wrong. */
stripped = (char*)malloc(strlen(input_string) + 1);

/* .. */
stripped++;

/* .. */
return stripped;

Try to keep a copy, something like original_stripped = stripped before starting to change stripped, and return the copied value (not the incremented one).

1
  • Thank you very much for this Jonathan. I knew that I was just about "there" but in my tiredness couldn't see that my malloc was a little off. I figured it might be.
    – diz
    Commented Sep 15, 2011 at 14:06
2

The problem is with calling stripped++. You are modifying the pointer you get by malloc. Make an extra pointer char *result_char = stripped; and use that for iteration over resulting string.

2

The problem ís that you increment your stripped variable before returning it. Try:

char *stripped; 
char *result;
stripped = (char*)malloc(strlen(input_string)*sizeof(char)); 
result = stripped;
...
return result; 
0

How about just:

    char* clean_string (char *input_string)
    {
      /*Ensure that input string isn't null and only do heavy lifting if it's not null*/
        if (input_string)
        {
            char *stripped;
            int i;

            stripped = (char*)malloc(strlen(input_string)*sizeof(char) + 1);

            for(i=0; i < strlen(input_string); i++)
                stripped[i] = (isalpha(input_string[i]) ? toupper(input_string[i]) : input_string[i]);

            return stripped;
        }
     /*default return val*/  


return NULL;
}
3
  • 1
    Even the 'isalpha' function call is useless. 'toupper' function automatically check whether the character is alphabetic.
    – Sanjeev
    Commented Sep 15, 2011 at 7:45
  • 'sizeof(char)' is useless as well (it is defined as being 1) Commented Sep 15, 2011 at 8:08
  • True but there is no overhead as compiler replaces it with appropriate char size (according to architecture) during compile time. While 'isalpha' will or won't be made inline according to the compiler decisions. Again, the code's prettier with 'sizeof(char)' (;
    – Sanjeev
    Commented Sep 15, 2011 at 8:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.