Compare two strings: Get all positions of differences

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sasidhar Parvatham

    Compare two strings: Get all positions of differences

    Hi All,

    How do I compare two strings and get all the positions where there is a
    difference between them?

    Thanks,
    Sasidhar


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Compare two strings: Get all positions of differences

    Sasidhar,

    You are going to have to do this manually. Basically, you would cycle
    through each character in one string, and compare it to the corresponding
    character in the other. If the string or the character doesn't exist, then
    there is a difference (I assume the strings will be of different lengths).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Sasidhar Parvatham" <sasidhar.parva tham@gmail.com> wrote in message
    news:OoqvkvlJFH A.2604@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hi All,
    >
    > How do I compare two strings and get all the positions where there is a
    > difference between them?
    >
    > Thanks,
    > Sasidhar
    >
    >[/color]


    Comment

    • Morten Wennevik

      #3
      Re: Compare two strings: Get all positions of differences

      Hi Sasidhar

      This will do it (hope I'm not doing your homework :) and if so, make sure
      you understand the code fully :P)

      ArrayList list = new ArrayList();
      string s = "Hello World";
      string t = "Getto Word";

      for(int i = 0; i < s.Length; i++)
      {
      if(i >= t.Length)
      list.Add(i);
      else if(s[i] != t[i])
      list.Add(i);
      }

      // if you also want virtual positions if s is the shortest string
      if(t.Length > s.Length)
      {
      for(int i = s.Length; i <= t.Length; i++)
      list.Add(i);
      }

      // optional, the list will contain all positions
      int[] indices = (int[])list.ToArray(t ypeof(int));

      --
      Happy Coding!
      Morten Wennevik [C# MVP]

      Comment

      • Sasidhar Parvatham

        #4
        Re: Compare two strings: Get all positions of differences

        Thanks Nocholas and Morten.

        I am aware of this method. I was looking for any other efficient way to do
        this. Your responses confirmed that there is no better way to do this than
        the one you guys suggested.

        I was looking whether there is a way we can use FC (DOS file compare) to do
        this or some other alternative.

        Anyhow thank you once again for your responses.
        Sasidhar


        "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
        news:opsnhg32x6 klbvpo@pbn_comp uter...[color=blue]
        > Hi Sasidhar
        >
        > This will do it (hope I'm not doing your homework :) and if so, make sure
        > you understand the code fully :P)
        >
        > ArrayList list = new ArrayList();
        > string s = "Hello World";
        > string t = "Getto Word";
        >
        > for(int i = 0; i < s.Length; i++)
        > {
        > if(i >= t.Length)
        > list.Add(i);
        > else if(s[i] != t[i])
        > list.Add(i);
        > }
        >
        > // if you also want virtual positions if s is the shortest string
        > if(t.Length > s.Length)
        > {
        > for(int i = s.Length; i <= t.Length; i++)
        > list.Add(i);
        > }
        >
        > // optional, the list will contain all positions
        > int[] indices = (int[])list.ToArray(t ypeof(int));
        >
        > --
        > Happy Coding!
        > Morten Wennevik [C# MVP][/color]


        Comment

        • Alexander Shirshov

          #5
          Re: Compare two strings: Get all positions of differences

          " A Generic, Reusable Diff Algorithm in C# "



          HTH,
          Alexander

          "Sasidhar Parvatham" <sasidhar.parva tham@gmail.com> wrote in message
          news:OMjf0VmJFH A.3628@TK2MSFTN GP15.phx.gbl...[color=blue]
          > Thanks Nocholas and Morten.
          >
          > I am aware of this method. I was looking for any other efficient way to do
          > this. Your responses confirmed that there is no better way to do this than
          > the one you guys suggested.
          >
          > I was looking whether there is a way we can use FC (DOS file compare) to
          > do
          > this or some other alternative.
          >
          > Anyhow thank you once again for your responses.
          > Sasidhar
          >
          >
          > "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
          > news:opsnhg32x6 klbvpo@pbn_comp uter...[color=green]
          >> Hi Sasidhar
          >>
          >> This will do it (hope I'm not doing your homework :) and if so, make sure
          >> you understand the code fully :P)
          >>
          >> ArrayList list = new ArrayList();
          >> string s = "Hello World";
          >> string t = "Getto Word";
          >>
          >> for(int i = 0; i < s.Length; i)
          >> {
          >> if(i >= t.Length)
          >> list.Add(i);
          >> else if(s[i] != t[i])
          >> list.Add(i);
          >> }
          >>
          >> // if you also want virtual positions if s is the shortest string
          >> if(t.Length > s.Length)
          >> {
          >> for(int i = s.Length; i <= t.Length; i)
          >> list.Add(i);
          >> }
          >>
          >> // optional, the list will contain all positions
          >> int[] indices = (int[])list.ToArray(t ypeof(int));
          >>
          >> --
          >> Happy Coding!
          >> Morten Wennevik [C# MVP][/color]
          >
          >[/color]


          Comment

          Working...