Converting Character Array to String

Discussion in 'C Programming' started by Charles L, Dec 3, 2003.

  1. Charles L

    Charles L Guest

    I don't know if this is a stupid quesiton or not.

    I would like to know how to convert an array of characters generated from a
    previous operation to a string ie how do I append a null character at the
    end? I haven't been able to do it so far. Is there a string function I can
    use?

    Can anyone help?

    Charles L
     
    Charles L, Dec 3, 2003
    #1
    1. Advertising

  2. Charles L

    lallous Guest

    "Charles L" <> wrote in message
    news:3fcdfe8e$...
    > I don't know if this is a stupid quesiton or not.
    >
    > I would like to know how to convert an array of characters generated from

    a
    > previous operation to a string ie how do I append a null character at the
    > end? I haven't been able to do it so far. Is there a string function I can
    > use?
    >
    > Can anyone help?
    >
    > Charles L
    >


    What is a string?
    Are you talking about STL's string ?

    an array of characters is a string.

    If you mean that array of characters doesn't have to be null terminated and
    string has to be, then the answer to your question would be:
    Try to determine a size out of your character array then do this:
    string[length] = 0;

    Regards
    Elias
     
    lallous, Dec 3, 2003
    #2
    1. Advertising

  3. Charles L

    Richard Bos Guest

    "Charles L" <> wrote:

    > I don't know if this is a stupid quesiton or not.
    >
    > I would like to know how to convert an array of characters generated from a
    > previous operation to a string ie how do I append a null character at the
    > end? I haven't been able to do it so far. Is there a string function I can
    > use?


    If you know how long the string is supposed to be, it's simple:
    array[length]='\0' will do the trick. If you don't know how long it's
    supposed to be, there's no feasible way to find out from the array
    alone, since the characters past the required length can be filled with
    anything, including seemingly normal text.

    Richard
     
    Richard Bos, Dec 3, 2003
    #3
  4. Charles L

    Eric Guest

    Charles L <> wrote:

    > I would like to know how to convert an array of characters generated from a
    > previous operation to a string ie how do I append a null character at the
    > end? I haven't been able to do it so far. Is there a string function I can
    > use?


    Do you track how many characters you have?

    If you know this, what you want to do is easy.

    long nCharacters; /*should be set to the number of characters you*/
    /*have*/

    yourString[nCharacters] = '\0';

    This does assume 'yourString' has the space to store the null character.
    If you do not know that it does, you will need to dynamically allocate a
    string that will.

    long nCharacters; /*should be set to the number of characters you*/
    /*have*/
    char *newString = NULL;

    newString = malloc( nCharacters + 1 ); /*should check for NULL*/
    strncpy( newString, yourString, nCharacters );
    newString[nCharacters] = '\0';


    If you do not know how many characters you have or cannot determine it,
    what you want to do is impossible.


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
     
    Eric, Dec 3, 2003
    #4
  5. Charles L

    Richard Bos Guest

    "lallous" <> wrote:

    > "Charles L" <> wrote in message
    > news:3fcdfe8e$...
    > > I don't know if this is a stupid quesiton or not.
    > >
    > > I would like to know how to convert an array of characters generated from

    > a
    > > previous operation to a string ie how do I append a null character at the
    > > end? I haven't been able to do it so far. Is there a string function I can
    > > use?

    >
    > What is a string?


    A string is, and I quote the Standard, "a contiguous sequence of
    characters terminated by and including the first null character".

    > Are you talking about STL's string ?


    Who is STL?

    > an array of characters is a string.


    Only if terminated by a null character.

    > If you mean that array of characters doesn't have to be null terminated and
    > string has to be,


    Of course he does.

    > Try to determine a size out of your character array


    How?

    Richard
     
    Richard Bos, Dec 3, 2003
    #5
  6. Charles L

    Lew Pitcher Guest

    lallous wrote:
    > "Charles L" <> wrote in message
    > news:3fcdfe8e$...
    >
    >>I don't know if this is a stupid quesiton or not.
    >>
    >>I would like to know how to convert an array of characters generated from

    >
    > a
    >
    >>previous operation to a string ie how do I append a null character at the
    >>end? I haven't been able to do it so far. Is there a string function I can
    >>use?
    >>
    >>Can anyone help?
    >>
    >>Charles L
    >>

    >
    >
    > What is a string?


    7.1.1 Definitions of terms
    1 A string is a contiguous sequence of characters terminated by and
    including the first null character. The term multibyte string is sometimes
    used instead to emphasize special processing given to multibyte characters
    contained in the string or to avoid confusion with a wide string. A pointer
    to a string is a pointer to its initial (lowest addressed) character. The
    length of a string is the number of bytes preceding the null character and
    the value of a string is the sequence of the values of the contained
    characters, in order.

    > Are you talking about STL's string ?


    He'd better not be, given that this is comp.lang.c and there's no such thing
    as STL here.

    > an array of characters is a string.


    Without the terminating null character, an array of characters is /not/ a
    string.

    > If you mean that array of characters doesn't have to be null terminated and
    > string has to be, then the answer to your question would be:
    > Try to determine a size out of your character array then do this:
    > string[length] = 0;


    Correct.

    > Regards
    > Elias
    >
    >



    --
    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
     
    Lew Pitcher, Dec 3, 2003
    #6
  7. Charles L

    Eric Guest

    Richard Bos <> wrote:

    > "Charles L" <> wrote:
    >
    > > I don't know if this is a stupid quesiton or not.
    > >
    > > I would like to know how to convert an array of characters generated from a
    > > previous operation to a string ie how do I append a null character at the
    > > end? I haven't been able to do it so far. Is there a string function I can
    > > use?

    >
    > If you know how long the string is supposed to be, it's simple:
    > array[length]='\0' will do the trick.


    Only if 'array' is big enough the hold the NULL character.

    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
     
    Eric, Dec 3, 2003
    #7
  8. in comp.lang.c i read:

    >I would like to know how to convert an array of characters generated from a
    >previous operation to a string ie how do I append a null character at the
    >end? I haven't been able to do it so far. Is there a string function I can
    >use?


    there is no string function you can use, because until there is a null byte
    at the end it's not a string, merely a sequence of characters.

    --
    a signature
     
    those who know me have no need of my name, Dec 4, 2003
    #8
  9. Charles L

    Capstar Guest

    Richard Bos wrote:
    > "lallous" <> wrote:
    >
    >
    >>"Charles L" <> wrote in message
    >>news:3fcdfe8e$...
    >>
    >>>I don't know if this is a stupid quesiton or not.
    >>>
    >>>I would like to know how to convert an array of characters generated from

    >>
    >>a
    >>
    >>>previous operation to a string ie how do I append a null character at the
    >>>end? I haven't been able to do it so far. Is there a string function I can
    >>>use?

    >>

    <snip>
    >
    >
    >>Try to determine a size out of your character array

    >
    >
    > How?


    By keeping track in the "previous operation"

    Mark
     
    Capstar, Dec 4, 2003
    #9
  10. Charles L

    Richard Bos Guest

    Capstar <> wrote:

    > Richard Bos wrote:
    > > "lallous" <> wrote:
    > >
    > >>Try to determine a size out of your character array

    > >
    > > How?

    >
    > By keeping track in the "previous operation"


    Well, quite. But that's not determining it "out of" the array.

    Richard
     
    Richard Bos, Dec 4, 2003
    #10
  11. Try using a variation of this code.

    It scans the contents of the array and replaces new-line or carriage
    returns with a null character.


    int count;


    for ( count = 0; count < strlen( genre ); count++ )
    {
    if ( genre[count] == '\n' || genre[count] == '\r' )
    {
    genre[count] = '\0';
    }
    }
     
    Andrew Robert, Dec 7, 2003
    #11
  12. Andrew Robert wrote:

    > Try using a variation of this code.
    >
    > It scans the contents of the array and replaces new-line or carriage
    > returns with a null character.
    >
    >
    > int count;
    >
    >
    > for ( count = 0; count < strlen( genre ); count++ )
    > {
    > if ( genre[count] == '\n' || genre[count] == '\r' )
    > {
    > genre[count] = '\0';
    > }
    > }


    Better:

    size_t count;
    size_t len = strlen(genre);
    for(count = 0; count < len; count++)
    {
    if(genre[count] == '\n' || genre[count] == '\r')
    {
    genre[count = '\0';
    }
    }

    Better still:

    char *p = strpbrk(genre, "\r\n");
    if(p != NULL)
    {
    *p = '\0';
    }

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Dec 7, 2003
    #12
  13. Charles L

    Joe Wright Guest

    Andrew Robert wrote:
    >
    > Try using a variation of this code.
    >
    > It scans the contents of the array and replaces new-line or carriage
    > returns with a null character.
    >
    > int count;
    >
    > for ( count = 0; count < strlen( genre ); count++ )
    > {
    > if ( genre[count] == '\n' || genre[count] == '\r' )
    > {
    > genre[count] = '\0';
    > }
    > }


    char *g = genre;
    int c;
    while ((c = *g++) != '\0')
    if (c == '\r' || c == '\n')
    *(g - 1) = '\0';
    --
    Joe Wright http://www.jw-wright.com
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Dec 7, 2003
    #13
  14. On Sun, 7 Dec 2003 08:37:09 +0000 (UTC), Richard Heathfield
    <> wrote:

    >Andrew Robert wrote:
    >
    >> Try using a variation of this code.
    >>
    >> It scans the contents of the array and replaces new-line or carriage
    >> returns with a null character.
    >>
    >>
    >> int count;
    >>
    >>
    >> for ( count = 0; count < strlen( genre ); count++ )
    >> {
    >> if ( genre[count] == '\n' || genre[count] == '\r' )
    >> {
    >> genre[count] = '\0';
    >> }
    >> }

    >
    >Better:
    >
    >size_t count;
    >size_t len = strlen(genre);
    >for(count = 0; count < len; count++)
    >{
    > if(genre[count] == '\n' || genre[count] == '\r')
    > {
    > genre[count = '\0';
    > }
    >}
    >
    >Better still:
    >
    >char *p = strpbrk(genre, "\r\n");
    >if(p != NULL)
    >{
    > *p = '\0';
    >}


    Andrew claimed he replaced all \n and \r but his code only replaces
    the first.

    "Better" replaces every \n or \r in the original string.

    "Better still" replaces only the first \n or \r.

    If it was your intent to demonstrate both capabilities, still better
    labels would have helped.


    <<Remove the del for email>>
     
    Barry Schwarz, Dec 8, 2003
    #14
  15. On Sun, 07 Dec 2003 08:37:09 +0000, Richard Heathfield wrote:

    > Andrew Robert wrote:
    >
    >> Try using a variation of this code.
    >>
    >> It scans the contents of the array and replaces new-line or carriage
    >> returns with a null character.
    >>
    >>
    >> int count;
    >>
    >>
    >> for ( count = 0; count < strlen( genre ); count++ )
    >> {
    >> if ( genre[count] == '\n' || genre[count] == '\r' )
    >> {
    >> genre[count] = '\0';
    >> }
    >> }

    >
    > Better:
    >
    > size_t count;
    > size_t len = strlen(genre);
    > for(count = 0; count < len; count++)
    > {
    > if(genre[count] == '\n' || genre[count] == '\r')
    > {
    > genre[count = '\0';
    > }
    > }
    >
    > Better still:
    >
    > char *p = strpbrk(genre, "\r\n");
    > if(p != NULL)
    > {
    > *p = '\0';
    > }


    Hmm. I haven't seen the original post, but the subject says "character
    array to string" - so I'd expect the original data to _not_ be strings,
    hence strpbrk, strlen and the like would be bad choices.
     
    Kelsey Bjarnason, Dec 8, 2003
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    693
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
    Jan 11, 2006
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    771
    those who know me have no need of my name
    May 28, 2006
  3. joy99
    Replies:
    7
    Views:
    851
    Blockheads Oi Oi
    Mar 26, 2011
  4. Replies:
    4
    Views:
    203
    Anthony Jones
    Feb 14, 2006
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    811
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page