Anagram Finder (This is acually a project in the C programming course and I would want it solved in C)

These are the steps I had to follow in the project, but im facing issues on what to do from step 8 onwards

Copy and paste the following starting code into your workspace:

#include<stdio.h>#include<string.h>int main() { }

Create two integer arrays called counter1 and counter2 . Initialize both arrays with four zeros each.

We will check if the following two strings are anagrams:

  • String 1: “dbb cccccaacb cdbababdcdcdab dcdad”
  • String 2: “bbbcc bdddccccad cdbbaaacaccdabdd”

Right under your counters, create two char arrays called s1 and s2 containing String 1 and String 2 respectively.

Counting the characters in String 1.

First, create a for loop to loop through the string. Use strlen() to find the length of the string.

We will now write the logic to determine the number of each character in String 1.

We will update counter1 in the following way:

  • If we encounter an ‘a’ we will increment counter1[0] by one.
  • If we encounter a ‘b’ we will increment counter1[1] by one.
  • If we encounter a ‘c’ we will increment counter1[2] by one.
  • If we encounter a ‘d’ we will increment counter1[3] by one.
  • If we encounter a space, we ignore it.

Implement this logic in the loop body.

Counting the characters in String 2.

Loop through String 2 and update its counter ( counter2 ) accordingly.

Comparing the counts of both strings.

We need a flag that we can use to determine if there is a mismatch in the two counters.

First, create an integer variable called flag and set it to 0 .

The flag variable maintains one of these states:

  • The flag will be set to zero if there is no mismatch in the counters.
  • The flag will be set to one if there is a mismatch in the counters.

Create an empty for loop that you will use to loop through both counters. We will place logic in it in the next task.

In the loop, change the flag variable’s value to 1 if a mismatch is encountered.

Well, are they anagrams?

If the two strings are anagrams, print “Anagram!”. If they are not, print “Not Anagram!”

Hey there @array3460115789! Welcome to the forums :slightly_smiling_face:

Let me make sure I’m understanding right. You’re stuck on making the for loop in step 8?
What part of this are you getting confused on?

Since it only contains the characters a - d we could actually hard code the for loop to have a variable that goes from 0 - 3. Or we could make it work for multiple sized arrays, a good chance to use sizeof.

Since sizeof returns the total size in bytes of an array we can divide it by the size of the type that area contains to get the length:

int nums = {1, 2, 3, 4};
long int = sizeof(nums) / sizeof(int);