How to rearrange the characters of same type

Posted by Rajasekhar0544 under C# on 12/10/2012 | Points: 10 | Views : 557 | Status : [Member] | Replies : 2
Dear All,

I have recently attended an interview, in that i came across this question:

Input string : ss bb za bd aa cx

output sting : aaa bbb c d ss x z

Please solve it..........

Thanks & Regards,
RajSekhar.




Responses

Posted by: Pavanandey on: 12/10/2012 [Member] Bronze | Points: 25

Up
0
Down
string Input = "ss bb za bd aa cx";
Input = Input.Replace(" ", "");
char[] In = Input.ToCharArray();
Array.Sort<char>(In);
StringBuilder OutPut = new StringBuilder();
char Ou = In[0];

for (int i = 0; i < In.Length; i++)
{
if (In[i] != Ou)
{
OutPut.Append(" ");
Ou = In[i + 1];
}
OutPut.Append(In[i]);
}

Thanks
Pavan Kumar
Mark Answer if this fits the need

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 12/11/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
a small modification to the above code , it has some mistakes

 string Input = "ss bb za bd aa cx";

Input = Input.Replace(" ", "");
char[] In = Input.ToCharArray();
Array.Sort<char>(In);
StringBuilder OutPut = new StringBuilder();
char Ou = In[0];

for (int i = 0; i < In.Length; i++)
{
if (In[i] != Ou ) // we should specify as array
{
OutPut.Append(" ");
Ou = In[i + 1];
}
}
OutPut.Append(In); // we should put this one out of the loop otherwise it will repeat the output
lblMeassage.Text = OutPut.ToString();



Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response