473,797 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2638
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...th e data
that i have is in the form of 1's and 0's
(10001010101010 101010000011... 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...@y ahoo.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...@y ahoo.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= 101010000011000 001, 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...@y ahoo.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= 101010000011000 001, 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...@y ahoo.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
8762
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 "i" and two "s". The function should also handle if String A and B have huge values, like two big dictionary. What's the best approach to achieve this with the best performance? what's the Big O then? I am thinking to put A and B into two...
32
49734
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(). Instead one has to iterate through the string, parse all UTF-8 multibytes and count each multibyte as one character. To address this problem the GTKmm bindings for the GTK+ toolkit have implemented a own string class Glib::ustring...
19
9523
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 have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
6
2130
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 i, j As Integer i = strtest.Compare(EvDt,StPeriod) Select Case (TaskType) Case "Old"
10
14489
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 the "right" (i.e.wysiwyg) answer while compare() does not.
14
24283
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 MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE)
11
2340
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 before the other. In a book I read they had used the second one but I prefer the first one because it it clearer.
11
4371
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 mistakes, i'm using cygwin on a windows pc and it seems that some functions cause stack overflows so am using fgets instead of fgetc (don't know y it solved the problem but that part is working). my problem now is that i am reading strings a few...
1
1755
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 mistakes, i'm using cygwin on a windows pc and it seems that some functions cause stack overflows so am using fgets instead of fgetc (don't know y it solved the problem but that part is working). my problem now is that i am reading strings a few...
5
3626
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 <fstream> using namespace std; string removeAllWhite( string eric) {
0
9536
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.