Delimit strings

This is a discussion on Delimit strings within the C Programming forums, part of the General Programming Boards category; This is my first time posting and I am not a strong C programmer so here is my problem. I ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    1

    Delimit strings

    This is my first time posting and I am not a strong C programmer so here is my problem.

    I am interfacing with a 3rd party software and the return from this app would be a space (or so I believe it is space) delimited string. I am having trouble replacing the "space" with a user defined delimiter.

    example -

    output string from 3rd party app:

    Code:
    name1 name2 name3
    What I would like to do is to convert the spaces between each to whatever the user has defined so it may look like -

    Code:
    name1,name2,name3
    I am using following code and it does not seem to work - special characters are being inserted into the string when I do the following -

    Code:
    for(i=0; i<strlen(buffer); i++) {
         if (buffer[i]==' ' && buffer[i-1] != ',') {
              tbuf[i]=',';
         }
         else 
              tbuf[i]=buffer[i];
    }
    Any ideas

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You should check if they really are spaces:
    Code:
    for(i = 0; buffer[i]; ++i)
      printf("%c (%d)\n", buffer[i], buffer[i]);
    If you don't see 32 printed for the spaces then they're something else. And this should at least tell you what they are.

    Also, doing evaluation strlen(buffer) each time through the loop is inefficient. Just look to see if the current character is the '\0' string terminator like I do in my loop.

    You're also accessing an out-of-bounds array element in your loop. If i is 0 you shouldn't check the i - 1 element.
    Last edited by itsme86; 03-15-2005 at 07:33 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Your question is similar to the eliminating spaces thread, Your output isn't being zero terminated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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