C Programming My program given two words should determine wh

C Programming

My program, given two words, should determine whether they are anagrams of each other. However, mine says all words are not anagrams of eachother, regardless of input. I think it has to do with my sortString function, but I am not sure how to go about assigning temp1 and temp2 with the word sorted alphabetically. Below is my code. Thank you for your time!

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

void anagram_check(char word1[], char word2[]);
void sort_string(char word[]);

void anagram_check(char word1[], char word2[]) { //this function checks whether two words are anagrams of each other
int i;
char temp1[20], temp2[20];
  
if (strlen(word1) != strlen(word2)) { //if strings are not of equal length, they cannot be anagrams
printf(\"%s is NOT an anagram of %s\ \", word1, word2);
exit(0);
}
  
for (i = 0; temp1[i]; i++) { //converting first string to lower case
temp1[i] = tolower(temp1[i]);
}
for (i = 0; temp2[i]; i++) { //converting second string to lower case
temp2[i] = tolower(temp2[i]);
}
  
sort_string(temp1); //calls of function to sort string alphabetically so they can easily be compared
sort_string(temp2);
  
if (strcmp(temp1, temp2) == 0) { //if string comparison equals 0, it is true that the words are anagrams
printf(\"%s is an anagram of %s\ \", word1, word2);
}
  
else if (strcmp(temp1, temp2) != 0) { //if string comparison does NOT equal 0, it is false that the words are anagrams
printf(\"%s is NOT an anagram of %s\ \", word1, word2);
}
}

void sort_string(char word[]) { //sorts strings alphabetically
char temp;
int i, j, length;
  
length = strlen(word); //
  
for (i = 0; i < length - 1; i++) { //compares letter to letter and sorts alphabetically, a-z
for (j = i + 1; j < length; j++) {
if (word[i] > word[j]) {
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
}
}
}
  

int main() {
  
char word1[20], word2[20]; //intializing to max 20 characters
  
printf(\"Please enter the first word: \");
scanf(\"%s\", word1);
  
printf(\"Please enter the second word: \");
scanf(\"%s\", word2);
  
anagram_check(word1, word2);
return 0;
}

Solution

PROGRAM CODE:

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#include <stdlib.h>

void anagram_check(char word1[], char word2[]);

void sort_string(char word[]);

void anagram_check(char word1[], char word2[]) { //this function checks whether two words are anagrams of each other

   //major thing is, when using variables for \'for loop\' declare them inside the loop.

   //everywhere i removed global declaration of i.

char temp1[20], temp2[20];

if (strlen(word1) != strlen(word2)) { //if strings are not of equal length, they cannot be anagrams

printf(\"%s is NOT an anagram of %s\ \", word1, word2);

exit(0);

}

//changed the for loop to populate temp array

for (int i = 0; i<strlen(word1); i++) { //converting first string to lower case

temp1[i] = tolower(word1[i]);

}

//changed the for loop to populate temp array

for (int i = 0; i<strlen(word2); i++) { //converting second string to lower case

temp2[i] = tolower(word2[i]);

}

  

sort_string(temp1); //calls of function to sort string alphabetically so they can easily be compared

sort_string(temp2);

  

if (strcmp(temp1, temp2) == 0) { //if string comparison equals 0, it is true that the words are anagrams

printf(\"%s is an anagram of %s\ \", word1, word2);

}

else if (strcmp(temp1, temp2) != 0) { //if string comparison does NOT equal 0, it is false that the words are anagrams

printf(\"%s is NOT an anagram of %s\ \", word1, word2);

}

}

void sort_string(char word[]) { //sorts strings alphabetically

char temp;

int length;

length = strlen(word);

for (int i = 0; i < length-1; i++) { //compares letter to letter and sorts alphabetically, a-z

for (int j = i + 1; j < length; j++) {

if (word[i]> word[j]) {

temp = word[i];

word[i] = word[j];

word[j] = temp;

}

}

}

}

int main() {

char word1[20], word2[20]; //intializing to max 20 characters

printf(\"Please enter the first word: \");

scanf(\"%s\", word1);

printf(\"Please enter the second word: \");

scanf(\"%s\", word2);

//some extra characters are read by scanf for the second element. removing them using this

for(int i=0; i<strlen(word2); i++)

{

   if(!isalpha(word2[i]))

   {

       word2[i] = \'\\0\';

   }

}

anagram_check(word1, word2);

return 0;

}

OUTPUT:

Please enter the first word: sa

Please enter the second word: as

sa is an anagram of as

Please enter the first word: err

Please enter the second word: rer

err is an anagram of rer

Please enter the first word: read

Please enter the second word: dear

read is an anagram of dear

Please enter the first word: ask

Please enter the second word: ink

ask is NOT an anagram of ink

C Programming My program, given two words, should determine whether they are anagrams of each other. However, mine says all words are not anagrams of eachother,
C Programming My program, given two words, should determine whether they are anagrams of each other. However, mine says all words are not anagrams of eachother,
C Programming My program, given two words, should determine whether they are anagrams of each other. However, mine says all words are not anagrams of eachother,
C Programming My program, given two words, should determine whether they are anagrams of each other. However, mine says all words are not anagrams of eachother,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site