Archived

This topic is now archived and is closed to further replies.

rob64464

comparing 2 strings (C#)

Recommended Posts

rob64464    100
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

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
rob64464    100
thank you! Ill see if that works! I managed to get both strings to char arrays! Never new about the sort command though.

Thank You,
Rob

Share this post


Link to post
Share on other sites
wyrd    112
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]

Share this post


Link to post
Share on other sites
rob64464    100
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]

Share this post


Link to post
Share on other sites
healeyx76    136
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]

Share this post


Link to post
Share on other sites
rob64464    100
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.

Share this post


Link to post
Share on other sites
SiCrane    11839
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();
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Ummm, did you ever try:

if (string1 == string2) {
// same
} else {
// not same
}

Share this post


Link to post
Share on other sites
rob64464    100
Hello,
this may seem like a noob question but I don''t call myself a noob and I don;t know it. How do I access static bool comp(string s1, string s2) from a private void statment?

Thanks,
Rob

Share this post


Link to post
Share on other sites
BlueDev    126
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]

Share this post


Link to post
Share on other sites
BlueDev    126
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

Share this post


Link to post
Share on other sites
SiCrane    11839
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");
}
}
}
}

Share this post


Link to post
Share on other sites