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 7593561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:12:03+00:00 2026-05-30T21:12:03+00:00

I’ve nearly finished my anagram solver program where I input two strings and get

  • 0

I’ve nearly finished my anagram solver program where I input two strings and get the result of whether they are anagrams of each other. For this example i’m using ‘Payment received’ and ‘Every cent paid me’.

The problem i’m getting is when I output the letterCount arrays, letterCount1 is incorrect (it doesn’t think there is a character ‘d’ but there is.) but letterCount2 is correct.

Can anyone see a problem with this because i’m completely baffled?

#include <stdio.h>
#include <string.h>

int checkAnagram(char string1[], char string2[])
{
        int i;
    int count = 0, count2 = 0;
    int letterCount1[26] = {0};
    int letterCount2[26] = {0};

    for(i = 0; i < strlen(string1); i++)
    {
        if(!isspace(string1[i]))
        {
            string1[i] = tolower(string1[i]);
            count++;
        }
    }

    for(i = 0; i < strlen(string2); i++)
    {
        if(!isspace(string2[i]))
        {
            string2[i] = tolower(string2[i]);
            count2++;
        }
    }

    if(count == count2)
    {
        for(i = 0; i < count; i++)
        {
            if(string1[i] >='a' && string1[i] <= 'z')
            {
                letterCount1[string1[i] - 'a'] ++;
            }

            if(string2[i] >='a' && string2[i] <= 'z')
            {
                letterCount2[string2[i] - 'a'] ++;
            }
        }

        printf("%s\n", string1);

        for(i = 0; i < 26; i++)
        {
            printf("%d ", letterCount1[i]);
            printf("%d ", letterCount2[i]);
        }
    }
}

main()
{
    char string1[100];
    char string2[100];

    gets(string1);
    gets(string2);

    if(checkAnagram(string1, string2) == 1)
    {
        printf("%s", "Yes");
    } else
    {
        printf("%s", "No");
    }
}
  • 1 1 Answer
  • 0 Views
  • 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-30T21:12:04+00:00Added an answer on May 30, 2026 at 9:12 pm

    I fixed it for you:

    #include <stdio.h>
    #include <string.h>
    
    int checkAnagram(char string1[], char string2[])
    {
        int i;
        int count = 0, count2 = 0;
        int letterCount1[26] = {0};
        int letterCount2[26] = {0};
        int len1 = strlen(string1);
        int len2 = strlen(string2);
    
        for(i = 0; i < len1; i++)
        {
            if(!isspace(string1[i]))
            {
                string1[i] = tolower(string1[i]);
                count++;
            }
        }
    
        for(i = 0; i < len2; i++)
        {
            if(!isspace(string2[i]))
            {
                string2[i] = tolower(string2[i]);
                count2++;
            }
        }
    
        if(count == count2)
        {
            for (i=0; i<len1; i++)
                if (!isspace(string1[i]))
                    letterCount1[string1[i]-'a']++;
            for (i=0; i<len2; i++)
                if (!isspace(string2[i]))
                    letterCount2[string2[i]-'a']++;
    
            int flag = 1;
            for(i = 0; flag && i < 26; i++)
                if (letterCount1[i] != letterCount2[i])
                    flag = 0;
            return flag;
        }
        return 0;
    }
    
    main()
    {
        char string1[100];
        char string2[100];
    
        gets(string1);
        gets(string2);
    
        if(checkAnagram(string1, string2) == 1)
        {
            printf("%s", "Yes");
        } else
        {
            printf("%s", "No");
        }
    }
    

    First, don’t calculate an string’s length inside a loop. I extracted them into len1 and len2 variables.

    Second, your loop was wrong! You shouldn’t go up to count, you should go up to that string’s length.

    Third, you didn’t return anything from checkAnagram function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I am using Paperclip to handle profile photo uploads in my app. They upload
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.