Click here to Skip to main content
15,914,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
In my web application, i have two strings and i need to display modified words and count between them.For example:


    string variable1="When you are writing server code you can never be sure what the IP address you see is refereeing to.In fact some users like it this way.";
    string variable2="When you are wrting server code yu cannn never be sure what the IP address you see is refering to.In fact some users like it this way";

    the result should be, modified words : wrting,yu ,cannn ,refering 
                          modified words count :4

how to do this can anyone tell me.

Thank you


What I have tried:

var result2 = variable2.Split(new char[] { ' ' }).Except(variable1.Split(new char[] { ' ' })).ToArray();

count = result2.Length;

Label3.Text = "Modified Word: " + result2[0].ToString() + "
" + "Modified Count: " + count.ToString();
Posted
Updated 24-Aug-16 1:10am

try this

C#
string variable1 = "When you are writing server code you can never be sure what the IP address you see is refereeing to.In fact some users like it this way.";
      string variable2 = "When you are wrting server code yu cannn never be sure what the IP address you see is refering to.In fact some users like it this way";


      string[] var1Words = variable1.Split(' ');
      string[] var2Words = variable2.Split(' ');


      string modified = "";
      List<string> lstModifiedWords = new List<string>();
      if (var1Words.Length == var1Words.Length)
      {
          for (int i = 0; i < var1Words.Length; i++)
              if (var1Words[i] != var2Words[i])
                  lstModifiedWords.Add(var1Words[i]);
      }
      modified = "Modified words:" + string.Join(",", lstModifiedWords) + " Modified Count:" + lstModifiedWords.Count;
 
Share this answer
 
v3
Comments
Ranjeeth Kumar Vootukuri 24-Aug-16 7:24am    
string s1="When you are writing server code you can never be sure what the IP address you see is refereeing to.In fact some users like it this way.";
string s2="are wrting server code yu cannn never be sure what the IP address you see is to.In fact some users like it this way";


the result should be, modified words : wrting,yu ,cannn ,refering
modified words count :4

I need to display like this can any one tell me
Thank you
Hi
you could split each string into an array as such, then check the counts of each array. Then check the counts to see which one is greater something has been changed in terms of length of words.

C#
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(string.Join(", ",Program.getDifferences("MY STRING IS GREAT", "MY STRING IS AWESOME")));
            Console.WriteLine(string.Join(", ", Program.getDifferences("MY STRING IS GREAT", "MY STR1NG IS GR3AT")));
            Console.ReadKey();

        }

        public static List<string> getDifferences(string StartString, string EndString)
        {
            //Split out
            IEnumerable<string> startStringSet = StartString.Split(' ');
            IEnumerable<string> endStringSet = EndString.Split(' ');

            if (StartString.Count() == 0 && endStringSet.Count() == 0)
            {
                //Return Empty List
                return new List<string>();
            }
            else if (startStringSet.Count() < endStringSet.Count())
            {
                //Return list excluding items in the End String
                return endStringSet.Except(startStringSet).ToList();
            }
            else
            {
                //Return list excluding items in the starting string
                return endStringSet.Except(startStringSet).ToList();
            }

        }
    }


Output Is:

AWESOME
STR1NG, GR3AT


Hope it helps

Regards
Dave
 
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