How do I make 2 strings 1 (without strcat)?

This is a discussion on How do I make 2 strings 1 (without strcat)? within the C++ Programming forums, part of the General Programming Boards category; Hi every1! I've got a question: How do I make 1 string from 2? For example, if I have one ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Question How do I make 2 strings 1 (without strcat)?

    Hi every1!

    I've got a question:
    How do I make 1 string from 2? For example, if I have one with the value "Ramon" and one with "van Dam", how can I combine them into a new one so that it's value will be "Ramon van Dam"? Oh, and without strcat? Thanx in advance!

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Why wouldn't you want to use strcat().

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    I thought you where gonna ask that I can't get it to work properly. I get errors like "cannot convert parameter 1 from 'char' to 'char *'" and stuff... have you got tips?

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Well if you are using null terminated character arrays, you could just loop through a smaller array (string) until the null character while placing each char into a larger array that will fit both your strings.

    Then from your counter, you would loop through the next array and write each char in to include the term char.

    To do it effectively, you would need to dynamically expand your concatenated array to keep from getting array overflows unless you were controlling string length (esp in the case of user input) with getline or something.

    My best tip is to find out why you cannot use strcat(). You should be able to effectively use it.

    char ch1[10]= "Blah";
    char ch2[10]= " Blah";

    strcat(ch1,ch2);

    cout << ch1;
    Last edited by Betazep; 02-03-2002 at 03:37 PM.
    Blue

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,637
    I get errors like "cannot convert parameter 1 from 'char' to 'char *'" and stuff...
    You must be declaring one of those strings as a char instead of a char * or char[]. Else you are just following your instructors demand not to use strcat
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    I got this mat. You put a bunch of conclusions on it and then you jump to them...

    It is called the "Jump To Conclusions Mat." (Office Space)

    Geeze gang. Don't slaughter the guy. Perhaps it is just a portion of his actual program that he needs to figure out.

    void I_DONT_DO_MY_C_EXERCISES_ON_MY_OWN( char *src, char *dest )
    {
    int i = strlen( src );
    while( *dest )
    src[i++] = *dest++;
    src[i] = 0;
    }

    main()
    {
    char str[20];
    strcpy( str, "hello ");
    char *str2 = "world";
    I_DONT_DO_MY_C_EXERCISES_ON_MY_OWN( str,str2 );
    }


    So you are going to write it for him and complain about it while you do it?
    Blue

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Thanx for the help, Betazep! Oh, and this is not an exercise for my class or something, it ís for my program, and this bit isn't working at the moment (but I've almost figured it out)

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,637
    My apologies, no offense...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Never mind. Anywayz, it's coming along pretty good after your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What to make?
    By Caldus in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2005, 01:12 PM
  2. Adding two strings to make a wchar_t
    By ElWhapo in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2005, 10:36 PM
  3. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  4. how to make files
    By quiksilver9531 in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2002, 05:44 PM
  5. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 11:26 AM

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