Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 595159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:01:54+00:00 2026-05-13T16:01:54+00:00

#include <iostream> #include <string> #include <algorithm> #include <cstdlib> #include <cstdio> using namespace std; static

  • 0
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstdio>

using namespace std;

static bool isanagram(string a, string b);

int main(void)
{
    int i,n,j,s;
    cin >> n;
    string a, b;
    cin >> a >> b;
    if(!isanagram(a,b)) cout << "False" << endl;
    else cout << "True" << endl;
    return 0;


}

static bool isanagram(string a, string b)
{
    int i, j, size, s=0;
    size = a.size();
    bool k;
    for(i=0;i<size;i++)
    {
        k=false;
        for(j=0;j<size;j++)
        {
            if(a[i] == b[j]) { k = true; break; }
        }
        if(k==true) s+=1;
    }
    cout << a[2] << b[2] << endl;
    if(s == size) return true;
    else return false;

}

I don’t know where exactly is the problem so i just pasted the whole code.

It should be a simple program capable for finding if two strings are anagrams, but it’s not working and i don’t know why. I used pointers in the program so thought the might be the problem and removed them, i removed other things additionally but still it’s not working. If you can give it a look-see and tell me some idea where i might’ve gone wrong with my code ?

Thank you in advance.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:01:54+00:00Added an answer on May 13, 2026 at 4:01 pm

    First things first: don’t declare the method static. It’s a confusing keyword at the best of times given all the roles it can fulfill… so reserve for times when you really have to (method or attribute of a class that is not tied to any instance for example).

    Regarding the algorithm: you’re nearly there, but presence only is not sufficient, you need to take the number of characters in account too.

    Let’s do it simply:

    bool anagram(std::string const& lhs, std::string const& rhs)
    {
      if (lhs.size() != rhs.size()) return false; // does not cost much...
    
      std::vector<int> count(256, 0); // count of characters
      for (size_t i = 0, max = lhs.size(); i != max; ++i)
      {
        ++count[lhs[i]];
        --count[rhs[i]];
      }
    
      for (size_t i = 0, max = count.size(); i != max; ++i)
        if (count[i] != 0) return false;
    
      return true;
    } // anagram
    

    Let’s see it at work: anagram("abc","cab")

    1. Initialization: count = [0, 0, ...., 0]
    2. First loop i == 0 > ['a': 1, 'c': -1]
    3. First loop i == 1 > ['a': 0, 'b': 1, 'c': -1]
    4. First loop i == 2 > ['a': 0, 'b': 0, 'c': 0 ]

    And the second loop will pass without any problem.

    Variants include maintaining 2 counts arrays (one for each strings) and then comparing them. It’s slightly less efficient… does not really matter though.

    int main(int argc, char* argv[])
    {
      if (argc != 3) std::cout << "Usage: Program Word1 Word2" << std::endl;
      else std::cout << argv[1] << " and " << argv[2] << " are "
                     << (anagram(argv[1], argv[2]) ? "" : "not ")
                     << "anagrams" << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
#include<fstream> #include<iostream> #include<string> #include<algorithm> using namespace std; int main(){ ifstream infile; ofstream outfile; infile.open(oldfile.txt);
#include<iostream> #include<string.h> #include<algorithm> using namespace std; pair<char[300],int> list[10000]; int main() { char a[300],b[20000]; int
Here's the code: #include <iostream> #include <string> #include <algorithm> using namespace std; int main()
#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
I have following code #include <iostream> #include <string> #include <algorithm> using namespace std; int
#include <string> #include <algorithm> #include <iostream> using namespace std; bool compare( const string::size_type i,
In my main.cpp: using namespace std; #include <cstdlib> #include <iostream> #include <set> #include <string>
#include <iostream> #include <string> using namespace std; string crash() { } int noCrash() {
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.