Skip to main content
gamedev.net
🔒 Locked

comparing 2 strings (C#)

Started by rob64464 15 replies 1k views
Original Post
rob64464
rob64464
Hello, I have to strings: string test1 = "bceasue" //notice bad spelling string test2 = "because" //notice correct spelling. ok, here is the problem. I am trying to detect is test1 has exactly the same letters as test2. How do I do it? Something to do with character arrays? Its just I am making a word game and I need to detect if a jumbled up letter actully makes a word. Thanks, Rob
SiCrane
SiCrane
My first instinct is to convert both strings to char arrays with String.ToCharArray(). Then sort both char arrays with Array.Sort() and then compare the char arrays for equality.
wyrd
wyrd
If you have to use char arrays for one reason or another, strings ARE char arrays. Just access each character via indexer as you usually would;

test1[0]
test1[1]
.. etc

EDIT:

You can also do a foreach loop;

foreach (char c in test1) {
// ..
}

[edited by - wyrd on March 22, 2004 5:51:01 PM]
rob64464
rob64464
That will not work because the word 'because' can change to any word in the dictionairy. Is there any command to split a string into characters and compare the 2 to see if they contain exactly the same characters?

[edited by - rob64464 on March 22, 2004 5:58:18 PM]
healeyx76
healeyx76
thats what sort is for. Wont that sort it numerically?
So Because becomes abceesu
& Beausce becomes abceesu then you loop at check SR1 == SR2[i]
rob64464
rob64464
Array.Sort(test1.ToCharArray());
Array.Sort(test2.ToCharArray());
MessageBox.Show(test1.ToString());
MessageBox.Show(test2.ToString());

that does not change the string. It still gives out bceause and because.
SiCrane
SiCrane
You''re converting the strings to temporary arrays, sorting the temporary arrays and throwing the temporary arrays away. You need to keep the arrays that you''ve sorted around. ex:

static bool comp(string s1, string s2)
{
char [] a1 = s1.ToCharArray();
char [] a2 = s2.ToCharArray();
Array.Sort(a1);
Array.Sort(a2);
return a1.ToString() == a2.ToString();
}
BlueDev
BlueDev
Well,

I would use strcmp in my opinion.

#include <stdio.h>

if( strcmp(test1, test2) == 0 ) {
// equal

}else {
// not equal

}


Simply how strcmp() works is it checks each byte as long as they are the same and if all match out it returns the offset which is 0. If you do != 0 then that will mean it is not equal.

Edit: For an example of how I think strcmp() was written is something like this:

int strcmp(const char *s1, const char *s2) {
while( *s1 == *s2 && *s1 && *s2 ) {
s1++;
s2++;
}

return *s1 - *s2;
}


Of course it might vary with the true standard library function, I've just tried writing one as simple as possible.

Edit: Ah, sorry I didn't see the (C#) part. I'll make another post if I can translate it. Sorry for the inconvenience.


Hope this helps,
[BDS]StackOverflow

[edited by - BlueDev on March 22, 2004 7:13:52 PM]
[/quote]
BlueDev
BlueDev
Ah,

Would it be possible to convert my strcmp() prototype to C#, then you could use it. I''m not sure how things go with C# but I think there are still for and while loops, including returns.

Maybe not, any C# programmers think it could be possible at all?


[BDS]StackOverflow
[/quote]
SiCrane
SiCrane
Porting strcmp() would be pointless. The C# string type already has a comparison method. Furthermore your strcmp() function doesn''t actually address the point of the original post.

As for accessing the comp() function, the simplest method is just put it in the same scope as the function you want to access it from. i.e.:

using System;

namespace mynamespace {
class Class1 {
static bool comp(string s1, string s2) {
char [] a1 = s1.ToCharArray();
char [] a2 = s2.ToCharArray();
Array.Sort(a1);
Array.Sort(a2);
return a1.ToString() == a2.ToString();
}

[STAThread]
static void Main(string[] args) {
if (comp("because", "becuase")) {
System.Console.WriteLine("same");
} else {
System.Console.WriteLine("different");
}
}
}
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.