Newbie: having a problem with my string function.

This is a discussion on Newbie: having a problem with my string function. within the C Programming forums, part of the General Programming Boards category; Hi everyone! I'm pretty new to C (been messing with it for about three weeks). I've been programming in Java ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    4

    Question Newbie: having a problem with my string function.

    Hi everyone! I'm pretty new to C (been messing with it for about three weeks). I've been programming in Java for about two years and before that, BASIC. So, I'm not really new to programming. To help me actually learn C I thought I would code some useful string functions. I successfuly wrote a substring function and now I'm trying to write a function that will remove all occurrances of a certain character in a string and return a copy of that string without the the specified character in it. So, here's my code:
    Code:
    /**This is in a file named mystring.c**/
    char *strsplit(char *string, char delimiter) {
    	int len = strlen(string);
    	char delimit = delimiter;
    	int i;
    	int j;
    	int numOfTokensFound = 0;
    	int newStringLen;
    	for(i = 0; i < len; i++) {
    		if(string[i] == delimit)
    			numOfTokensFound++;
    	}
    	newStringLen = len - numOfTokensFound;
    	char output[newStringLen];
    	for(i = 0, j = 0; string[i]; i++, j++) {
    		if(string[i] == delimit)
    			output[j] = string[i];
    	}
    	output[j] = '\0';
    	return output;
    }
    In my main program:
    Code:
    #include "mystring.h"
    ...
    ...
    main()
    {
          ...
          char string[] = "Hello, there!"
          char remove = 'e';
          printf("The string returned by strsplit is:\n%s\n", strsplit(string, e));
          return 0;
    }
    When I run the program I see that strsplit returns some odd characters. I don't see where I'm going wrong. I would be gratefull if you guys can point me in the right direction.
    Also, I'm using gcc3.3 on Mac OS 10.2.6 (don't know if that matters);
    Anyways, thanks for your time.

  2. #2
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    I have a feeling that string is a reserved word, meaning you cannot use it for a string.

    Surely the best way to go would be to use 2 strings, and pointers. That way you could increment the pointer to the next letter in the string, check the letter against the delimiter, IF it is the same, just increment the pointer without copying anything accross, IF IT ISN'T the same, copy the letter accross to the second string.

  3. #3
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >I have a feeling that string is a reserved word, meaning you cannot use it for a string.
    Nope, unless its in the global namespace where anything starting with str is reserved. The name strsplit is reserved because anything in the string.h header may add stuff.

    >To help me actually learn C
    Just my opinion, but it looks like you're learning C99. I'd recommend not doing that because C99 isn't used much at all and you need to know how it's done in C89 because there's bunches and bunches of C89 code drifting around. Anyway, there's a simpler way to do it using pointers.
    Code:
    Mon Oct 20 9:00:06am
    sangut: ~/misc
    > <c.c less
    /**This is in a file named mystring.c**/
    char *strsplit(char *string, char delimiter) {
      char *s;
      char *p;
    
      s = p = string;
      while (*s != '\0') { /* '\0' is the end of the string */
        if (*s != delimiter) /* Is it the delimiter? */
          *p++ = *s; /* No? Copy it to the final location */
        s++; /* Go to the next one */
      }
      *p = '\0'; /* Close the string */
    
      return string;
    }
    
    int main ( ) {
      char string[] = "Hello, there!";
      char remove = 'e';
    
      printf("The string was |%s|\n", strsplit(string, remove));
    
      return 0;
    }
    But your code has a single distinct problem:

    >char output[newStringLen];
    This is a variable local to the strsplit function. When you return, the memory for output is released and it'll probably have garbage in it. Those are probably your funny characters. If you want a variable to stick around between functions you can do one of several things: pass a buffer as an argument to strsplit, dynamically allocate anonymous memory with malloc/calloc, declare the string as static within strsplit, make it global. By the way, that's the order that you should try those suggestions too. They get progressively worse.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    4

    Thanks

    Thanks guys. I'll try what you guys suggested when I get home. (at school right now). Also, does that K & R book use C 89? I have the book, Teach Yourself C in 24 Hours.
    Anyways, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21