473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

String compare

Hi !!
i want to compare two strings and return the difference

For example, i have two strings,
string str1="then";
string str2="than";

it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??
Mar 12 '08 #1
6 2623
On Wed, 12 Mar 2008 09:20:41 -0700 (PDT), Muhs <sh******@gmail.com>
wrote:
>Hi !!
i want to compare two strings and return the difference

For example, i have two strings,
string str1="then";
string str2="than";

it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??
Have a look at the Levenshtein Distance measure,
http://en.wikipedia.org/wiki/Levenshtein_distance which may be what
you are looking for. As far as I am aware it is not in .NET, though
it is possible there is a C# version of the algorithm out there
somewhere.

rossum

Mar 12 '08 #2
On Mar 12, 4:20 pm, Muhs <shumy...@gmail.comwrote:
Hi !!
i want to compare two strings and return the difference

For example, i have two strings,
string str1="then";
string str2="than";

it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??
I don't think so.

This is quite easy, so I suspect it is homework. You won't learn if
you get somebody else to write the code for you. If you have written
some code and are having trouble, then post it with a description of
the problem and we can probably help.

If it is not homework, then what is the application? (I can think of
a number of issues with the specification as it stands, and the
resolution will depend on the application.)
Mar 12 '08 #3
lol.. i know what u mean by being it a homework... its not exactly a
homework..
its my degree project which i'll be working on till september... and
ive just stated.. My project is about mutation clustering...the data
that i have is in the form of 1's and 0's
(10001010101010101010000011... could be int or string). And i have to
cluster them based on the hamming distance...
I was just thinking of ways to compare and cluster the data... (i
thought i could compare the each string and getthe number of different
bits)
or i can XOR each int and find the distance...
somebody in the reply above sent a link... and i found this hamming
distance algo:

int dist = 0;
int val = 0;
int x= 8;
int y=15;
val = x ^ y; // this will be equal to 7

while(val!=0)
{
++dist;
val = val - 1;
}

but this

On 12 Mar, 16:43, Martin Bonner <martinfro...@yahoo.co.ukwrote:
On Mar 12, 4:20 pm, Muhs <shumy...@gmail.comwrote:
Hi !!
i want to compare two strings and return the difference
For example, i have two strings,
string str1="then";
string str2="than";
it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??

I don't think so.

This is quite easy, so I suspect it is homework. *You won't learn if
you get somebody else to write the code for you. *If you have written
some code and are having trouble, then post it with a description of
the problem and we can probably help.

If it is not homework, then what is the application? *(I can think of
a number of issues with the specification as it stands, and the
resolution will depend on the application.)
Mar 12 '08 #4
int distance = 0;
int val = 0;
int x= 8;
int y=15;
val = x ^ y;

while(val!=0)
{
++distance;
val = val - 1;
}

but the distance that i get is 7, same as val... i was thinking of
converting 7 to binary and count the number of 1's (111) that way i
could get distance 3..

On 12 Mar, 16:43, Martin Bonner <martinfro...@yahoo.co.ukwrote:
On Mar 12, 4:20 pm, Muhs <shumy...@gmail.comwrote:
Hi !!
i want to compare two strings and return the difference
For example, i have two strings,
string str1="then";
string str2="than";
it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??

I don't think so.

This is quite easy, so I suspect it is homework. *You won't learn if
you get somebody else to write the code for you. *If you have written
some code and are having trouble, then post it with a description of
the problem and we can probably help.

If it is not homework, then what is the application? *(I can think of
a number of issues with the specification as it stands, and the
resolution will depend on the application.)
Mar 12 '08 #5
lol.. i know what you mean by being it a homework... actually its my
final year degree project.. on which i'll be working on till
sepetember... my project is about mutation clustering... and i have
data in the form of 1's and 0's(data= 101010000011000001, could be int
or string)... now i want to cluster the data based on some similarity
measure.... i thought i could compare the string data and find the
number of characters that are different... but somebody above sent a
link and there was this algo of hamming distance... i wrote the algo
as follows:

int distance = 0;
int val = 0;
int x= 8;
int y=15;
val = x ^ y;

while(val!=0)
{
++distance;
val = val - 1;
}
but the distance that i get is 7, same as val... i was thinking of
converting 7 to binary and count the number of 1's (111) that way i
could get distance 3..




On 12 Mar, 16:43, Martin Bonner <martinfro...@yahoo.co.ukwrote:
On Mar 12, 4:20 pm, Muhs <shumy...@gmail.comwrote:
Hi !!
i want to compare two strings and return the difference
For example, i have two strings,
string str1="then";
string str2="than";
it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??

I don't think so.

This is quite easy, so I suspect it is homework. *You won't learn if
you get somebody else to write the code for you. *If you have written
some code and are having trouble, then post it with a description of
the problem and we can probably help.

If it is not homework, then what is the application? *(I can think of
a number of issues with the specification as it stands, and the
resolution will depend on the application.)
Mar 12 '08 #6
On Mar 12, 5:13 pm, Muhs <shumy...@gmail.comwrote:
lol.. i know what you mean by being it a homework... actually its my
final year degree project.. on which i'll be working on till
sepetember... my project is about mutation clustering... and i have
data in the form of 1's and 0's(data= 101010000011000001, could be int
or string)... now i want to cluster the data based on some similarity
measure.... i thought i could compare the string data and find the
number of characters that are different... but somebody above sent a
link and there was this algo of hamming distance... i wrote the algo
as follows:

int distance = 0;
int val = 0;
int x= 8;
int y=15;
val = x ^ y;

while(val!=0)
{
++distance;
val = val - 1;
}
but the distance that i get is 7, same as val... i was thinking of
converting 7 to binary and count the number of 1's (111) that way i
could get distance 3..
Well yes. As currently coded, the while loop is equivalent to:
distance = val;
val = 0;
(... provided x,y are constrainted to be >= 0). Go look at the
wikipedia page again, and code the updated correctly, and it will
indeed count the bits.
>

On 12 Mar, 16:43, Martin Bonner <martinfro...@yahoo.co.ukwrote:
On Mar 12, 4:20 pm, Muhs <shumy...@gmail.comwrote:
Hi !!
i want to compare two strings and return the difference
For example, i have two strings,
string str1="then";
string str2="than";
it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??
I don't think so.
This is quite easy, so I suspect it is homework. You won't learn if
you get somebody else to write the code for you. If you have written
some code and are having trouble, then post it with a description of
the problem and we can probably help.
If it is not homework, then what is the application? (I can think of
a number of issues with the specification as it stands, and the
resolution will depend on the application.)
Mar 13 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
6
by: Maileen | last post by:
Hi, I have the following code : Function GetRequestType(ByVal EvDt As String, ByVal StPeriod As String, ByVal EdPeriod As String, ByVal TaskType As String) As Integer Dim strtest As String Dim...
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
1
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.