-3

The if statement doesn't terminate the while loop for some reason, which results in unwanted outputs (more than one output). I hope someone can help. Thanks.

my code

tash29
  • 1
  • 4
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] to show us, *in the question itself*. And don't show images of text, copy-paste it *as text*. – Some programmer dude Jul 27 '19 at 06:44
  • I also recommend that you learn [how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). And perhaps about the `break` statement. – Some programmer dude Jul 27 '19 at 06:47
  • Look at end of if statement. Else statement do nothing. And it will go back to for loop if bool_ = false. You should add break to exit the the loop – sycoi001 Jul 27 '19 at 06:48
  • 1
    Finally, please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jul 27 '19 at 06:48
  • This code `bool_ = false;` terminates the **outer** loop, there's nothing in the code you've written to terminate the **inner** loop. The inner loop is where you get the output from. – john Jul 27 '19 at 07:04
  • Why not use [std::is_permutation](https://en.cppreference.com/w/cpp/algorithm/is_permutation)? – Jesper Juhl Jul 27 '19 at 10:35

3 Answers3

0

I think you should return false inside if. Try this code.

#include <bits/stdc++.h> 
using namespace std; 


bool areAnagram(string str1, string str2) 
{ 

    int n1 = str1.length(); 
    int n2 = str2.length(); 


    if (n1 != n2) 
        return false; 


    sort(str1.begin(), str1.end()); 
    sort(str2.begin(), str2.end()); 


    for (int i = 0; i < n1; i++) 
        if (str1[i] != str2[i]) 
            return false; 

    return true; 
} 


int main() 
{ 
    string str1 = "test"; 
    string str2 = "ttew"; 
    if (areAnagram(str1, str2)) 
        cout << "The two strings are anagram of each other"; 
    else
        cout << "The two strings are not anagram of each other"; 

    return 0; 
} 
  • 1
    Please don't just post code without explanation of what makes it different from the code the OP posted. Explanatory answers are always better than code to copy-paste, as code-only answers tend to lead to [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming) which is bad. – Some programmer dude Jul 27 '19 at 06:50
0

First, your if statement doesn't break the inner loop. That means that you would output the phrase "NOT ANAGRAM" the number of times the sorted arrays differ. Actually, the number of times minus one (probably), because of the second: the inner loop compares one element less than it should. You should iterate upto i < len. Third, doesn't matter if it is an anagram or not, the program will always output "ANAGRAM" at the end because you set bool_ = false right after this output.

Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
0

@tash29 and @D.Khumoyun

std::string has a == operator. You can compare a string by simply writing if (a == b). There is no need for byte wise comparison.

If you use the C++ ternary conditional operator, you can do everything important in one line.

I am sorry, the task is too simple, I cannot explain more . . .

Please see:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string a = "ajar";
    std::string b = "stag";

    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());

    std::cout << ((a == b) ? "Anagram" : "Not Anagram");

    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44