Anagram Finder | C (Arrays and Strings)

These are the steps for Anagram finder, but i’m facing issues on what to do from step 9. (My entire code is at the bottom and commented out)

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.

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

** Use an if statement to check if the i’th element in counter1 matches the i’th element in counter2 . If an element in counter1 does not match its counterpart in counter2 , set flag to 1 .

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

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

int main() {

  // create two integer arrays called counter1 and counter2 and initialize both arrays with four zeros each.
  int counter1[] = {0, 0, 0, 0,};
  int counter2[] = {0, 0, 0, 0,};
  

  // create two char arrays called s1 and s2 containing String 1 and String 2 respectively.
  // String 1: “dbb cccccaacb cdbababdcdcdab dcdad”
  // String 2: “bbbcc bdddccccad cdbbaaacaccdabdd”
  char s1[] = "dbb cccccaacb cdbababdcdcdab dcdad";
  char s2[] = "bbbcc bdddccccad cdbbaaacaccdabdd";


    // need a flag that we can use to determine if there is a mismatch in the two counters.
  // create an integer variable called flag and set it to 0.
  // the flag will be set to zero if there is no mismatch in the counters.
  int flag = 0;


  // create a for loop to loop through the string1; use strlen() to find the length of the string.
  for (int i = 0; i < strlen(s1); i++) {
    // If we encounter an ‘a’ we will increment counter1[0] by one.
    if (s1[i] == 'a') {
      counter1[0]++;
    }
    // If we encounter a ‘b’ we will increment counter1[1] by one.
    if (s1[i] == 'b') {
      counter1[1]++;
    }
    // If we encounter a ‘c’ we will increment counter1[2] by one.
    if (s1[i] == 'c') {
      counter1[2]++;
    }
    // If we encounter a ‘d’ we will increment counter1[3] by one.
    if (s1[i] == 'd') {
      counter1[3]++;
    }
    // If we encounter a space, we ignore it.
    if (s1[i] == ' ') {
      continue;
    }
  }


 // create a for loop to loop through the string2; use strlen() to find the length of the string.
  for (int j = 0; j < strlen(s2); j++) {
    // If we encounter an ‘a’ we will increment counter1[0] by one.
    if (s2[j] == 'a') {
      counter2[0]++;
    }
    // If we encounter a ‘b’ we will increment counter1[1] by one.
    if (s2[j] == 'b') {
      counter2[1]++;
    }
    // If we encounter a ‘c’ we will increment counter1[2] by one.
    if (s2[j] == 'c') {
      counter2[2]++;
    }
    // If we encounter a ‘d’ we will increment counter1[3] by one.
    if (s2[j] == 'd') {
      counter2[3]++;
    }
    // If we encounter a space, we ignore it.
    if (s2[j] == ' ') {
      continue;
    }
  }


  // create an empty for loop that you will use to loop through both counters.
  for (int k = 0; k < 4; k++) {
    // THIS IS SPECIFICALLY WHERE I AM HAVING AN ISSUE
   // AS I UNDERSTAND, IF MY TWO ARRAYS DO NOT MATCH, FLIP FLAG TO 1
    if (counter1[k-1] != counter2[k-1]) {
      // the flag will be set to one if there is a mismatch in the counters.
      flag = 1;
    } else {
      break;
    }
  }

  // if the two strings are anagrams, print “Anagram!”
  if (flag == 1) {
    printf("Not Anagram!\n");
    } else { //if they are not, print “Not Anagram!”
    printf("Anagram!\n");
    }

}

Any feedback would be appreciated!

Hey!
Im stuck on the same! Did you figure it out?

Unfortunately, not yet. But I will attempt it once more.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.