Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 comparing 2 strings
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HeavenAgain




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: 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
haskell




PostPosted: Wed Feb 28, 2007 6:03 am   Post subject: RE:comparing 2 strings

Regular expressions anyone?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: