DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    h2ombre Guest

    storing more than one word in char arrays


    How can you store more than one word in a char array?

  2. #2
    Danny Kalev Guest

    Re: storing more than one word in char arrays

    how about:

    char arr[] = "hello world";

    Danny

    h2ombre wrote:
    >
    > How can you store more than one word in a char array?


  3. #3
    rmcc Guest

    Re: storing more than one word in char arrays


    "h2ombre" <bwbrown@ukans.edu> wrote:
    >
    >How can you store more than one word in a char array?

    If the char array is big enough, use strcat from string lib functions.


    #include <string.h>
    #include <stdio.h>

    void main( void )
    {
    char string[80];
    strcpy( string, "Hello world from " );
    strcat( string, "strcpy " );
    strcat( string, "and " );
    strcat( string, "strcat!" );
    printf( "String = %s\n", string );
    }



  4. #4
    Guest

    Re: storing more than one word in char arrays


    "h2ombre" <bwbrown@ukans.edu> wrote:
    >

    To do this you must use the cin.getline() function. for example if you wish
    to hold "John Doe" in Name[30]; you must say
    cout << "Please enter your name : ";
    cin.getline(){Name,30,'\n');
    note that '\n' will be the default character. good luck

    *Traveling the world!!
    >How can you store more than one word in a char array?



  5. #5
    Richard Vickers Guest

    Re: storing more than one word in char arrays


    "h2ombre" <bwbrown@ukans.edu> wrote:
    >
    >How can you store more than one word in a char array?


    Use a second EOS indicator (say @) and check each string.
    Store "Easy@Fix" in the char array....upon retrieval, check
    the string for the @...characters before are first array,
    characters after are second array. @ in first position
    indicates Empty first string. @ in last position indicates
    Empty second string. String length 1 AND @ in first position,
    both strings Empty. Just a thought while reading.

    Richard Vickers
    rvicke@earthlink.net


  6. #6
    jan Guest

    Re: storing more than one word in char arrays


    "h2ombre" <bwbrown@ukans.edu> wrote:
    >
    >How can you store more than one word in a char array?


    The responses so far are all correct if you are storing words and not phrases,
    which is the more general problem. The classic solutions to this problem
    take the form of using the null character between phrases and a double null
    character at the end to signal the end of the set of phrases. So, the following
    is a set of phrases. \0 is the null character.

    char *pStr = "Phrase 1 - has all sorts of !@#$# characters\0Phrase 2: another
    one\0\0";

    Parsing this is frightfully easy. Just use the pointer as is for the entire
    first string, then increment the pointer by the length of the string plus
    the null, then use it again until the pointer points to a null.

    while (pStr)
    {
    printf("Phrase: %s\n", pStr);
    pStr += strlen(pStr) + 1;
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links