Thread: combine array elements

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    combine array elements

    I have two strings and want to intertwain the elements into an new array. Does anyone know of a way to do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How exactly do you want to combine them?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    for example:

    string 1 = code
    string 2 = area

    I want the result string to be cAoRdEeA

    but i am having a difficult time coming up with the loop.

    Any help?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You would want to loop from 0 to the size of the smaller string, right? Start with that.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    yeah, but I don't know how to include the other characters from the other string in betwwen. I am completely stuck at this point. Can anyone offer a suggestion?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    // pseudo code
    Code:
    for each element in the shorter (astring, bstring)
       result += one letter from astring + one letter from bstring;
    if lengths aren't equal add remaining in the longer (astring, bstring) to result
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    the loop would skip an array location, right? (0, 2 4, etc). That's enough clues.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    matsp, I am drawing a blank as I have never coded this before:

    what would be the update on the (for?) loop for your suggestion?

    How can I skip an aray location????

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    this is what I have:
    Code:
    for (b = 0; b < str3.length(); b++)
    	{
    		result[b] = str3[b];
    		result[b + 2] = str3[b+1];
    	}
    str3 is string 1...

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since you are using C++ strings, you don't need to worry about indexing the destination.

    But you do need to grab a character from each of the two strings. Your code is only taking from the variable called "str3".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    str3 is not a variable, it is a string.

    How could I do this without indexes. I am in need of urgent help.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    What about this:
    Code:
    for (b = 1; b < str3.length(); b++)
    	{
    		result[0] = str3[0];
    		result[b + 2] = str3[b];
    	}
    
      
    
    	result [totlength] = 0;
    	cout << result << endl;
    if I have "are", I will get
    a==re=
    for the cout.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    what about this:
    Code:
    
    int a = 0;
    	 int l = 1;// initialize the Array Indices
         int b = 0;
         int c = 0;
    	result[0] = str3[0];
    	result[1] = str2[0];
    	for (b = 1; b < str3.length(); b++)
    	{
    		result[a + 2] = str3[b];
    		a++;
    	}
    	for (c = 1; c < str2.length(); c++)
    	{
    		result[l + 2] = str2[c];
    		l++;
    	}
      
    
    	result [totlength] = 0;
    	cout << result << endl;

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Ok, i got it merging them for equal lengths. Now, my question is, how do I tell the program that if the second string is shorter , just to print out the rest of string 1? using the structure I used above?
    here is what i have:

    Code:
    int a = 0;
    	 int l = 1;// initialize the Array Indices
         int b = 0;
         int c = 0;
    	result[0] = str3[0];
    	result[1] = str2[0];
    	for (b = 1; b < str3.length(); b++)
    	{
    		result[a + 2] = str3[b];
    		a++;
    		a++;
    	}
    	for (c = 1; c < str2.length(); c++)
    	{
    		result[l + 2] = str2[c];
    		l++;
    		l++;
    	}
      
    
    	result [totlength] = 0;
    	cout << result << endl;
    }

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    how can I address this? anyone?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM