Anagram Finder | C

Unsure how to correct my code here, regardless of what I input it always reads an anagram.

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

int flag = 0;
int counter1 = {0, 0, 0, 0};
int counter2 = {0, 0, 0, 0};
char s1 = “abcd”;
char s2 = “abcd”;

for (int i = 0; i < strlen(s1); i++){
if (s1[i] = ‘a’){
counter1[0]++;
}
else if (s1[i] = ‘b’){
counter1[1]++;
}
else if (s1[i] = ‘c’){
counter1[2]++;
}
else if (s1[i] = ‘d’){
counter1[3]++;
}
else
counter1;
break;
}
for (int i = 0; i < strlen(s2); i++) {
if (s2[i] = ‘a’) {
counter2[0]++;
}
else if (s2[i] = ‘b’) {
counter2[1]++;
}
else if (s2[i] = ‘c’) {
counter2[2]++;
}
else if (s2[i] = ‘d’) {
counter2[3]++;
}
else
counter2;
break;
}
for (int i = 0; i < 4; i++) {
if (counter1 || counter2) {
flag++;
printf (“Not an anagram!\n%d”, flag);
break;
}
else
printf(“Anagram!”);
break;
}
}

Have a look at this guide on how to format code in forum posts:

Here are a few issues you should consider:

  • In your if conditions, you have (s1[i] = 'a'). You want to compare the two characters, not make an assignment. Therefore, == should be used instead of =. The conditions should be similar to (s1[i] == 'a'). All the other if conditions also need to be corrected.

  • I don’t quite follow what the else is supposed to do.

else 
counter1;
break;
// Same issue with the counter2 loop as well.

You can delete the above from your code. The statement counter1; doesn’t do anything. Also, since you have chosen to omit curly braces { } from the else block, so only one single statement belongs to the else block.

// Your code above is equivalent to:
else  {
counter1;
}
break;

Since the break; is outside the else block but inside the for loop, you will exit the for loop after the first iteration i.e. you will only process the first character of the string only. You could use curly braces for the else block and move the break; inside the curly braces. But as I said, you can delete the else and break statements. If you think they are necessary and serve some purpose, share your reasoning and intent for this portion of your code.

  • The condition if (counter1 || counter2) doesn’t make much sense. You are using the logical or || operator. Why? I think the not equal operator != would be appropriate. Also, you want to check whether the counter for any of the characters doesn’t match, so you should use the condition if (counter1[i] != counter2[i])

  • The placement of printf("Anagram!"); and break; at the bottom is not correct. You should delete the else and break;. If you don’t do so, then only the counter for the first character 'a' will be checked. If the counters are equal for 'a', then you will print Anagram! and break out of the loop without checking the counters for 'b', 'c', 'd'.
    Instead you want to let the for loop run to completion. If one of the counters doesn’t match, you will print “Not an anagram” and break out of the for loop which is what you want. The printf("Anagram!") statement should be moved outside the for loop. Outside the for loop, you can have an if condition to check the value of flag. If the flag is still 0, it means the for loop completed without any mismatch between the character counts and you can print `Anagram!"