Computer Science Canada

comparing 2 strings

Author:  HeavenAgain [ Tue Feb 27, 2007 11:32 pm ]
Post subject:  comparing 2 strings

umm this is a question straight from the junior question for the CCC
is called anagram checker
instructions are :
input : the program should prompt the user for two phrases, each entered on a separate line. you may assume that the input only contains upper case letters and spaces.
output: the program will print out one of the two statements :"is an anagram" or is not an anagram

example would be "TIME" and "ITEM" or "CS AT WATERLOO" and "COOL AS WET ART"
those 2 would be anagram, i'm assuming the spaces doesnt matter.

i thought about this question and i used a hash map to solved it. but is there any other ways?

Author:  Martin [ Wed Feb 28, 2007 12:39 am ]
Post subject:  RE:comparing 2 strings

Something like this:

code:

string word = "...";
string anag = "...";
int letters[26] = {0};
for( int i=0;i<word.length( ); ++i )
   if( word[i] is a letter )
       ++letters[ word[i]'s alphabet position];

for( int i=0;i<anag.length( ); ++i )
   if( anag[i] is a letter )
       --letters[ anag[i]'s alphabet position];

if( word[0] through word[25] == 0 )
   word is an anagram
else
   not an anagram

Author:  haskell [ Wed Feb 28, 2007 6:03 am ]
Post subject:  RE:comparing 2 strings

Regular expressions anyone?


: