Strings in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • randomstring
    New Member
    • Mar 2012
    • 3

    Strings in C

    Hello. I'm new to C, and trying to figure out why some code I wrote isn't working. The code is supposed to receive a telephone number that could be formatted with digits, parenths, or dashes. It is supposed to strip out the parenths and dashes, retaining just the digits. Here's the code:

    Code:
    char* formatNum (char* validNum) {
        
        int counter;
        int index;
        int length;
        char* numOut;
        char validChar;
        
        counter = 0;
        index = 0;
        length = strlen (validNum);
        numOut = malloc ((length + 1) * sizeof(char));
        
        while (counter <= length) {
            validChar = validNum[counter];
            if (validChar != '-' && validChar !='(' && validChar != ')') {
                numOut[index] = validChar;
                index++;
            } // end if
            counter++;
        } // end while 
        
        numOut[index] = '\0';
        
        return numOut;
        
    } // end formatNum
    Even though the calling function is passing a properly validated string, this function returns an empty string. Can someone help me figure out what's going on?

    I know there are other ways of accomplishing the desired result but I am curious about why this isn't working. Thanks for your help!
    Last edited by randomstring; Mar 13 '12, 09:22 AM. Reason: Added tag for C programming
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Part from line 14 where the test should be < not <= I see no issues with your code and when called like this

    Code:
    int main()
    {
        char in[] = "(123)-456-789";
        char *out = formatNum(in);
        
        printf("%s -> %s\n", in, out);
        
        free(out);
    
        return 0;
    }
    it seems to work for me; output:

    (123)-456-789 -> 123456789

    The issue with line 14 is that you test the zero terminator because you go 1 index too far along the string and as a result you put 2 zero terminators into the output string. This in it self is not a problem but for an input string containing only digits you would end up writing an output string that was 1 longer than the code expected and so it would write outside the bounds of the object pointed to by numout. Remember if the length of a string is N then the characters are at indexes 0 - N-1.

    Comment

    • randomstring
      New Member
      • Mar 2012
      • 3

      #3
      Thanks, Banfa! I fixed the error you pointed out on line 14.

      Meanwhile, I'm sorry to report that the code still isn't working for me at all. As I step through the code line by line, the debugger shows that if validNum is (123)456-7890, then as the function executes, on line 10 validNum changes to (123)456-7. On line 11, it changes to (123)45, and so on. Ultimately what gets returned is a string with just the second char "1". Since those lines don't even reference validNum, I'm not sure why the string is getting clobbered. Perhaps the problem is with how I'm calling it? The function call is:

      Code:
      numOut = formatNum (validNum);
      where both numOut and validNum are char*s. I see that in your sample code you used char in[] instead of char*. Should I be defining the input string as an array instead of a char*? If so, why?

      I really appreciate your patience and help. Thanks!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No your function works equally well if I declared in as char* in = "(123)-456-789";

        That really suggests that the error is in the rest of the code, how do you call this function? How do you generate the string to pass to it?

        Comment

        • randomstring
          New Member
          • Mar 2012
          • 3

          #5
          Banfa, you're right, my input string was a pointer to something that didn't exist past the function that generated it. (Grr, dumb error that took hours to debug and two seconds to fix.) Thanks for all your help! I much appreciate your looking at my code and helping me fix/debug it.

          Comment

          Working...