🔒 Locked
comparing 2 strings (C#)
Advertisement
Original Post
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
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();
}
Well,
I would use strcmp in my opinion.
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:
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]
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]
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.:
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.
Advertisement