string manipulation

Hi, I have searched this long and hard and don't seem to see another post on this issue.
I have two strings each with the same characters but in a different order.

String1=�word�
String2=�dwor�

I want to test them to show their similarity. Unfortunately I can't do a sort so that they will be identical (dorw) as that only works on file. Can someone propose a non sed/awk way of doing this.

Thanks

C.J

Please indicate how you're defining "similarity"

sorry I meant identical. This is for the purposes of a test. The strings are already very similar. They only differ in the way of content by order. I want to run a command(s) on them so that they look the same.

but they can look the same in many different permutations, 96 I think for a four letter word. How do you know which permutation you want them to be identical too?

Can you a give a permutation for one that can be put in a test command?

I was hoping to manipulate them so they both are identical.

E.g. sort characters in order

string1=dorw
string2=dorw

They are identical and can be put in a test command, returning true. The strings already have the same length and letters so I thought there must be some way to get them like that.

Hi,

My solution is very awkward anc complex, but it works well in my PC.

So just try it, to be honest, i do not know how to sort a single string. Seems the split/sort/join are all working for a file but not a single string.

If you have found any good solution, do let me know. i am really curious about this thread.

echo "first string"
read str1
echo "second string"
read str2
echo $str1 | sed 's/./& /g' > temp1
echo $str2 | sed 's/./& /g' > temp2
for i in `cat temp1`
do
echo $i
done | sort > t1
for j in `cat temp2`
do
echo $j 
done | sort > t2
a=`comm -3 t1 t2 | wc -l`
if [ $a -le 0 ]
then
echo "similiar"
else
echo "different"
fi
rm temp1 temp2 t1 t2

I'm not sure what you want to do. Do you want to proof the equality of the strings? eg. same length, same chars, same order? Or do you want to put one string of which you already have proven length and same chars in the same order as the first string? Or don't care about the order of either of them, as long as they're in the same order, whatever that order is?

firstly, you can try to indent your code to make it more readable for the OP. secondly

could be equivalent to

 sort temp1 > t1 

.

In GNU awk , if you have the words in different files:

awk 'NR==FNR{for(i=1;i<=NF;i++)a1[$i]=$i}
{for(j=1;j<=NF;j++){a2[$j]=$j;if (!($j in a1)){f=1}}}
END {for ( o in a1 )if (!( o in a2 )){f=1}
    if (f || ( i != j )) {print "different"}else{print "similar"}
}' FS='' f1 f2
#!/usr/bin/perl
use warnings;
use strict;
unless (@ARGV == 2)  {
   die "Usage: perl scriptname.pl word1 word2";
}
my $test =  (test(@ARGV)) ? 'true' : 'false';
print $test;
sub test {
   return 1 if join('',sort (split('',shift))) eq join('',sort (split('',shift)));
   return 0;
}