469,327 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

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
Nov 16 '05 #1
4 27518
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]
- mv*@spam.guard.caspershouse.com

"Sasidhar Parvatham" <sa****************@gmail.com> wrote in message
news:Oo**************@TK2MSFTNGP15.phx.gbl...
Hi All,

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

Thanks,
Sasidhar

Nov 16 '05 #2
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(typeof(int));

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
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" <Mo************@hotmail.com> wrote in message
news:opsnhg32x6klbvpo@pbn_computer...
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(typeof(int));

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

Nov 16 '05 #4
" A Generic, Reusable Diff Algorithm in C# "

http://codeproject.com/csharp/DiffEngine.asp

HTH,
Alexander

"Sasidhar Parvatham" <sa****************@gmail.com> wrote in message
news:OM**************@TK2MSFTNGP15.phx.gbl...
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" <Mo************@hotmail.com> wrote in message
news:opsnhg32x6klbvpo@pbn_computer...
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(typeof(int));

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


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Fazer | last post: by
2 posts views Thread by Nathan Rosaaen | last post: by
3 posts views Thread by Twinkle | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.