Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find(Print) the duplicate char in this line in asc order using for loop only.

For eg
string repeatedWord = "I want to find the duplicate char in this line in asc order";

Please note that: NO Linq, No inbuilt method.

-Update

I have done to find out the char in a string For eg:

C#
string word = "I want to find the duplicate char in this line in asc order";
string hold = "";
for (int i=0; i< word.Length; i++)
    {
    for(int j=i+1; j< word.Length ; j++)
        {
        
        if(word[i].ToString() == " ")
            {
            break;
            }
        if(word[i] == word[j])
            {
            hold = hold + word[i];
            break;
            }
        
        }
    }

Console.WriteLine(hold);


Now I need to sort it in a asc order
Posted
Updated 6-Aug-15 22:49pm
v2
Comments
Patrice T 7-Aug-15 3:37am    
What is the question ?
[no name] 7-Aug-15 3:55am    
I have done to find out the char in a string For eg:
string word = "I want to find the duplicate char in this line in asc order";
string hold = "";
for (int i=0; i< word.Length; i++)
{
for(int j=i+1; j< word.Length ; j++)
{

if(word[i].ToString() == " ")
{
break;
}
if(word[i] == word[j])
{
hold = hold + word[i];
break;
}

}
}

Console.WriteLine(hold);

Now I need to sort it in a asc order
Patrice T 7-Aug-15 6:57am    
I have improved your question with your source code.
You can do it directly.

Sort it.
If your homework won't let you use the inbuilt sort methods, then write a quick bubble sort - it's pretty trivial.
Then, loop through the sorted array comparing consecutive characters - the first time the two match, you have found the duplicate you are seeking.
 
Share this answer
 
I would do it like this :
Iterate through the string and build a List (of Char) from the Characters in the String. Here you must look if the List allready contains the Char before you add it again.
Now you build another List or an Array which corresponds to the Character-List.
You iterate once more through the String and increase the Integer-Array at that Position (Index) which corresponds to the Char-List.
 
Share this 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