delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/08/27/16:47:42

Date: Mon, 27 Aug 2001 22:56:59 +0200
From: Andrea Mazzoleni <am AT mediacom DOT it>
To: djgpp AT delorie DOT com
Subject: Re: Program, uses compare() to check for anagrams, but doesnt work
Message-ID: <20010827225659.A2180@mediacom.it>
References: <3B8AEFEF AT MailAndNews DOT com>
Mime-Version: 1.0
User-Agent: Mutt/1.2.5i
In-Reply-To: <3B8AEFEF@MailAndNews.com>; from Ramiman12@MailAndNews.com on Mon, Aug 27, 2001 at 01:29:08PM -0400
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Mon, Aug 27, 2001 at 01:29:08PM -0400, Ramy Elmeligy wrote:
>    for( ; a < greater, equal == 0 ; a++)
>      {
>       input1.substr(a, b).compare(input2, c, b);
>      }
This is an infinite loop. equal is always 0. Probably your intention was to
write something like :

for( ; a < greater && equal == 0 ; a++)
	equal = input1.substr(a, b).compare(input2, c, b);


Anyway this is a simpler implementation:

----------------------------------------------
#include <algorithm>
#include <string>

using namespace std;

bool is_anagram(string a, string b) {
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        return a == b;
}

int main() {
        cout << is_anagram("aba","baa") << endl;
        return 0;
}
----------------------------------------------
					
-- 
Andrea Mazzoleni <am AT mediacom DOT it>
935A 2D3C 5C70 BCD6 CB0C  ED89 7C19 4321 6340 3F6D

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019