Click here to Skip to main content
15,920,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to compare two strings. when i given two strings for example abcd decg.
it should iterate all the characters in a string and it should display how many characters are repeated.

i am not getting idea. please help me

thank you.
Posted

There are a couple of ways to do it:

1) Set up two loops, one inside the other and go through each string. This is easy to do, but can be slow and may give you "silly" results if the same character is repeated in each string. I would call this the "brute-force-and-ignorance" approach.
2) Sort each string into alphabetical order, and then set up a single loop to compare them.
2a) If the character in stringA is less than the character in string B, move stringA on one character
2b) If stringB is greater, than string A, move stringB on.
2c) If they are the same, count it, and move them both.
 
Share this answer
 
Comments
ProEnggSoft 4-Mar-12 11:55am    
Second alternative is good. My 5.
I solved my question thank you for your help.
class Program
    {
        static void Main(string[] args)
        {
            string a = "suresh";
            string b = "ramesh";
            char[] d = a.ToCharArray();
            char[] e = b.ToCharArray();
            int k = 0;
            for (int i = 0; i < 6; i++)
            {
                for (int s = 0; s < 6; s++)
                {
                    if (d[i] == e[s])
                    {
                        Console.WriteLine(d[i]);
                        k = k + 1;
                    }
                }
            }
            Console.WriteLine("The Equal Number of Characters in two Strings:-" + k);
            Console.ReadLine();
        }
    }
 
Share this answer
 
v3
//Result will show b,c,d,g,i
string[] roles = { "a", "e", "f", "h" };
string[] allRoles = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };

foreach (string nextRole in allRoles)
{
if (Array.IndexOf(roles, nextRole) == -1)
{
Response.Write(nextRole + "<br/>");
}
}
 
Share this answer
 
v2

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