Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
string k = "ABC"; 
string l = "DEF"; 
string result = "ADBECF";


C#
using System;

namespace ADP_PG2_1_Solution
{
	public class MixStringsExercise
	{
		public static string MixStrings(string s, string t) {
			//Example : s= ABC, t = DEF ==> ADBECF 
			string L ;
			string next ;
			if (s.Length == t.Length) {
				L = s + t;
					} else {
						return null;
						}
						for(int i = 0; i <= s.Length; i ++ ){
				return s = 
							}
				
		
			return null;
		}
	}
}
Posted
Updated 28-Apr-16 20:56pm
v2
Comments
Member 12485203 2-May-16 3:40am    
thanks a lot . Al of u

The question although makes no sense, however, the string results seem as if you want to use 1 character from each and then get the result by incrementing the counter and appending the next characters from both arrays of characters (which, strings are).

C#
var builder = new StringBuilder();
string a = "ABC", b = "DEF";

for (int i = 0; i < a.Length; i++) {
   builder.Append(a[i]);
   builder.Append(b[i]);
}

Console.WriteLine(builder.ToString());

// Output:
// ADBECF


I used StringBuilder (as Sergey had suggested in the comments to Solution 1) to get a better performance because I don't have to worry about the concatenation here. However, the algorithm still has some problems because it works on arrays of same length; try to use an array with less indices.
 
Share this answer
 
Comments
Karthik_Mahalingam 29-Apr-16 0:51am    
5, simple and clear
You have several choices to concat two strings:
C#
string s1 = "ABC";
string s2 = "DEF";

// Using + operator
string r1 = s1 + s2;

// Using String.Concat
string r2 = String.Concat(s1, s2);

// Using String.Format
string r3 = String.Format("{0}{1}", s1, s2);

// Using string literal (C# 5)
string r4 = $"{s1}{s2}";

// Using a StringBuilder (a bit overkill for this trivial task)
string r5 = new StringBuilder(s1.Length + s2.Length).Append(s1).Append(s2).ToString();

You can also copy the characters one by one in a buffer.
C#
char[] chars = new char(s1.Length + s2.Length);
int index = 0;
for (int i = 0; i < s1.Length; i++) {
   chars[index++] = s1[i];
}
for (int i = 0; i < s2.Length; i++) {
   chars[index++] = s2[i];
}
string r6 = new String(chars);


[Edit] Sorry for I misread your question. You can achieve it bu only slightly modifying the code block above:
C#
char[] chars = new char(s1.Length + s2.Length);
int index = 0;
for (int i = 0; i < s1.Length; i++) {
   chars[index] = s1[i];
   index += 2;
}
index = 1;
for (int i = 0; i < s2.Length; i++) {
   chars[index] = s2[i];
   index += 2;
}
string output = new String(chars);
 
Share this answer
 
v2
Comments
Patrice T 28-Apr-16 16:43pm    
I fear none of the solutions is what the OP is looking for.
Bonjour Olivier,
le OP veux entrelacer les 2 chaînes. boir l'exemple.
phil.o 28-Apr-16 16:45pm    
Oups :) I completely misread the desired output. Thanks for pointing it out.
Sergey Alexandrovich Kryukov 28-Apr-16 17:34pm    
I voted 4 this time. It would be good to give some judgement on the cost and reasons for different approaches, conduct the idea that strings are immutable and warn about repeated string concatenations, which makes string.Format or StringBuilder beneficial.
—SA
phil.o 29-Apr-16 0:56am    
You are perfectly right Sergey, a 4 is fair. Thank you.
Karthik_Mahalingam 29-Apr-16 0:49am    
+5, for different methods of string concatenation.
Couple of ways are there to complete your task
1. using 'StringBuilder'
C#
StringBuilder strBuilder= new StringBuilder();
string str1 = "str1";
string str2 = "str2";
//concate using string builder
strBuilder.Append(str1);
strBuilder.Append(str2);
//print it
Console.WriteLine(strBuilder.ToString());

2. String.Concat Method
It Concatenates two specified instances of String.
C#
string fName = "testFname";
string lName = "testLname";
string szOut =  string.Concat(fName, lName)
 
Share this answer
 
v2
Comments
Raje_ 29-Apr-16 3:11am    
I think you missed the point. Please check other solutions. :)
koolprasad2003 29-Apr-16 3:24am    
Just checked....
glad you got your answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900