Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have two strings "Rahul" ,"Mishra" i want to show like
frist letter of frist string then first letter of second string ...then 2 letter of first string ....

ex-RAHUL
MISHRA

output:
RMAIHSUHLRA
Posted
Comments
Nelek 21-Aug-14 5:14am    
This is not how CP usually works. Most important goal here is to learn and help learning.
You are supposed to try it on your own, and come here when you got stuck with something, with a concrete question about your code, design, etc.
Please have a look to What have you tried?[^] to see a good explanation about what I mean.
Don't forget people here don't get payed. And besides, if we give you a ready-to-go solution, it is not going to help you because you are not going to learn anything from it.



This task is quite trivial. Try it and you will discover it is not so difficult

 
Share this answer
 
C#
using System.Linq;

static void Main(string[] args)
{
    string mixed = Interleave("RAHUL", "MISHRA");
}

public static string Interleave(string s1, string s2)
{
    StringBuilder sb = new StringBuilder();
    s1.Zip(s2, (c1, c2) => sb.Append(c1).Append(c2)).Count();   // .Count() to force the Lazy evaluation!
    if (s1.Length > s2.Length)
    {
        sb.Append(s1.Substring(s2.Length));
    }
    else if (s2.Length > s1.Length)
    {
        sb.Append(s2.Substring(s1.Length));
    }
    return sb.ToString();
}
 
Share this answer
 
Comments
Jameel VM 25-Aug-14 2:00am    
there is some performance issue in your algorithm. my algorithm perform faster than you.
Rahulmishra011 26-Aug-14 5:46am    
there is not matter of performance issue.just i want try it without loops
i have created an algorithm for u . Please try this
C#
public static string GetString(string string1,string string2)
      {
          char[] str1 = string1.ToCharArray();
          char[] str2 = string2.ToCharArray();

          string finalString = "";
          int length = str1.Length > str2.Length ? str2.Length : str1.Length;
          for (int i = 0; i < length ; i++)
          {
              finalString = finalString + str1[i] + str2[i];
          }
          if (str1.Length > str2.Length)
          {
              int difference = str1.Length - str2.Length;
              for (int i = 0; i < difference; i++)
              {
                  char rem = str1[str2.Length + i];
                  finalString = finalString + rem;
              }

          }
          else
          {
              int difference = str2.Length - str1.Length;
              for (int i = 0; i < difference; i++)
              {
                  char rem = str2[str1.Length + i];
                  finalString = finalString + rem;
              }
          }
          return finalString;
      }


Pass your string like below
C#
string finalString = GetString("RAHUL", "MISHRA");

Hope this helps
 
Share this answer
 
v2
Comments
Jameel VM 21-Aug-14 5:47am    
I have update my answer please try the new one
Rahulmishra011 21-Aug-14 5:48am    
thank sir i more thing sir i can use this without loop?? it is possible ?
Jameel VM 21-Aug-14 5:55am    
Without loop we can't achieve it..because how can we get alternative character without index? also this algorithm will work with any character length you want.
Jameel VM 21-Aug-14 6:05am    
why you are trying to do it without loop? any specific reason?
Rahulmishra011 21-Aug-14 6:15am    
no no no specific reason just i want try it

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