Results 1 to 12 of 12

Thread: Junction 2 strings

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    Junction 2 strings

    how would u do this?
    i have the following strings:

    char *str1 = "123";
    char *str2 = "456";

    and now i want to print "123456"
    what is the best way to do that?
    \m/\m/

  2. #2
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    How about that:

    Code:
    printf("%s%s", str1, str2);
    VS.NET 2003

    Need to email me?

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    or you could use the standard string class
    Code:
    #include <string>
    #include <iostream>
    using std::string;
    using std::cout;
    
    // ....
    string one = "part one";
    string two = "and";
    string three = "two";
    
    one += ' ' + two + ' ' + three + '.';
    cout << one;
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    do ppl use string type instead of using char arrays? i want to know what the pro guys do lol :P

    made_of_asp: well imagine i want to work out that char array..using printf wont work here then

    anyways..there is here a thing that does confusion to me........how much spaces/slots in a char array u usually put? 30? 60? 200? the performance difference is big?
    \m/\m/

  5. #5
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Originally posted by sunburnt
    or you could use the standard string class
    Code:
    #include <string>
    #include <iostream>
    using std::string;
    using std::cout;
    
    // ....
    string one = "part one";
    string two = "and";
    string three = "two";
    
    one += ' ' + two + ' ' + three + '.';
    cout << one;
    you can also do

    Code:
    sprintf(szString, "%s%s", string1, string2);
    String class is very heavy and is part of STL. I mostly write C-style code, so i can't use it.
    VS.NET 2003

    Need to email me?

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    so i should avoid using the string class?
    \m/\m/

  7. #7
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    so i should avoid using the string class?
    No, that's what it's for. The standard library's string class is not really slower then c strings, sometimes even faster. It is also much less error prone. By using string you don't have to worry about allocating and freeing memory, whether or not you need to append a '\0', etc. So, unless you either a. Must use plain C; or b. Are a wizard programmer writing kernel code, use std::string.

  8. #8

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tks all
    in which case its faster than c strings(char arrays)?
    \m/\m/

  9. #9

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by sunburnt
    or you could use the standard string class
    Code:
    #include <string>
    #include <iostream>
    using std::string;
    using std::cout;
    
    // ....
    string one = "part one";
    string two = "and";
    string three = "two";
    
    one += ' ' + two + ' ' + three + '.';
    cout << one;
    that isnt workin on vc++7...what should i change?
    \m/\m/

  10. #10
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    You shouldn't really avoid STL. But it does increase file size (greatly).

    I can do everything in STL with plain C, so I don't really need it. I don't think string class is faster than char arrays, it has an overhead. But I might be wrong.
    VS.NET 2003

    Need to email me?

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't think string is ever faster either. But it is written to cause very little runtime overhead.
    And its ease of use makes up for any disadvantages.
    I always use string except when I'm into absolute speed, and I seldom am.

    that isnt workin on vc++7...what should i change?
    Working for me if I put the actual code in main.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmmm ok then ill investigate about the string class
    \m/\m/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width