比较两个字符串 [英] Comparing two strings

查看:119
本文介绍了比较两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较两个字符串.当我给出两个字符串时,例如abcd decg.
它应该迭代字符串中的所有字符,并应该显示重复多少个字符.

我没有主意.请帮助我

谢谢.

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.

推荐答案

有两种方法可以做到:

1)设置两个循环,一个在另一个循环内,并遍历每个字符串.这很容易做到,但可能会很慢,并且如果在每个字符串中重复相同的字符,可能会给您愚蠢"的结果.我将其称为暴力与无知"方法.
2)将每个字符串按字母顺序排序,然后设置一个循环以进行比较.
2a)如果字符串A中的字符小于字符串B中的字符,则将字符串A移至一个字符
2b)如果stringB大于string A,则将stringB继续.
2c)如果它们相同,则将其计数并同时移动它们.
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.


检查以下内容:

http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/fbh501kz%28v = vs.80%29.aspx [ ^ ]

希望对您有所帮助:)
check these out:

http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx[^]

http://msdn.microsoft.com/en-us/library/fbh501kz%28v=vs.80%29.aspx[^]

hope it helps :)


我解决了我的问题,谢谢您的帮助.
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();
        }
    }


这篇关于比较两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆