'A LIST ANAGRAM WORD PAIRS USING 2d ARRAY
I'm trying to do an exercise which is an anagram.
Sample input: GARDEN DANGER WHO HOW DO ODD
SAMPLE OUTOUT: A LIST OF ANAGRAM WORD PAIRS.
WHO : HOW
DANGER : GARDEN
this the error:
main.c:24:6: warning: incompatible pointer to integer conversion assigning to 'char' from 'char [10]' [-Wint-conversion] ch = word[i];
main.c:38:15: warning: comparison between pointer and integer ('char *' and 'int') if( word[i ]== 1){
main.c:39:19: warning: more '%' conversions than data arguments [-Wformat] printf("%s : %s", word[i]);
Here is my code:
#include <stdio.h>
int main() {
char word[5][10];
char ch;
int counts1[26];
int counts2[26];
int i = 0 , n = 5;
printf("Input a word: ");
//Read the user input
while(i <= n){
scanf("%s[^\n]", word[i]);
i++;
}
//Count the characters
for(i = 0; i < 26; ++i) {
counts1[i] = 0;
counts2[i] = 0;
}
i = 0;
while(word[i] != 0) {
ch = word[i];
if(ch >= 'A' && ch <= 'Z') {
ch += 32;
}
counts1[ch-'a']++;
i++;
}
i = 0;
for(i = 0; i < 26; ++i) {
if(counts1[i] != counts2[i]) {
return 0;
}
}
//Check if Anagram or not
if( word[i]== 1){
printf("%s : %s", word[i]);
}
else{
printf("%s is not an anagram", word[i]);
}
}
Solution 1:[1]
In :
while(word[i] != 0) {
ch = word[i];
word[i] is an array of chars (not an int) and ch is a char.
And :
if( word[i]== 1){
means nothing : you compare an array of char (word[i]) with an integer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ptit Xav |
