J'essaie d'implémenter un testeur d'anagrammes en C. Lors de l'appel du programme, l'utilisateur entre deux mots entre guillemets, comme "listen" et "silent". J'ai presque réussi à le faire fonctionner, mais j'ai quelques problèmes avec une fonction d'aide que j'ai écrite pour éliminer les espaces dans les deux mots entrés. Voici le code de cette fonction :
void noSpaces(char word[100]) {
/*
This is a function to get rid of spaces in a word
It does this by scanning for a space and shifting the
array elements at indices > where the space is
down by 1 as long as there is still a space
there.
*/
for (int i = 0; i < 100; i++) {
while (word[i] == ' ') {
for (int j = i; j < 100; j++) {
word[j] = word[j+1];
}
}
}
}
Maintenant, cela fonctionne bien lorsque je passe le mot d'entrée de la fonction main
à cette aide. Le problème se situe au niveau du deuxième appel à cette fonction. Lorsque j'appelle cette fonction sur la deuxième entrée, si k
est le nombre d'espaces dans la première entrée, alors la fonction efface la première entrée. k
lettres de la deuxième entrée. Par exemple, en tapant ./anagram " banana" "banana"
me donnera un faux négatif, et si j'ajoute une instruction print pour voir ce qui se passe avec les entrées après noSpaces
est appelé sur eux, j'obtiens ce qui suit :
banana
anana
Voici le code pour le programme complet :
#include <stdio.h>
int main(int argc, char *argv[]) {
//this if statement checks for empty entry
if (isEmpty(argv[1]) == 0 || isEmpty(argv[2]) == 0) {
//puts("one of these strings is empty");
return 1;
}
//call to noSpaces to eliminate spaces in each word
noSpaces(argv[1]);
noSpaces(argv[2]);
//call to sortWords
sortWords(argv[1]);
sortWords(argv[2]);
int result = compare(argv[1], argv[2]);
/*
if (result == 1) {
puts("Not anagrams");
} else {
puts("Anagrams");
}
*/
return result;
}
int compare(char word1[100], char word2[100]) {
/*
This is a function that accepts two sorted
char arrays (see 'sortWords' below) and
returns 1 if it finds a different character
at entry i in either array, or 0 if at no
index the arrays have a different character.
*/
int counter = 0;
while (word1[counter] != '\0' && word2[counter] != '\0') {
if (word1[counter] != word2[counter]) {
//printf("not anagrams\n");
return 1;
}
counter++;
}
// printf("anagrams\n");
return 0;
}
void sortWords(char word[100]) {
/*
This is a function to sort the input char arrays
it's a simple bubble sort on the array elements.
'sortWords' function accepts a char array and returns void,
sorting the entries in alphabetical order
being careful about ignoring the 'special character'
'\0'.
*/
for (int j = 0; j < 100; j++) {
int i = 0;
while (word[i + 1] != '\0') {
if (word[i] > word[i + 1]) {
char dummy = word[i + 1];
word[i + 1] = word[i];
word[i] = dummy;
}
i++;
}
}
}
void noSpaces(char word[100]) {
/*
This is a function to get rid of spaces in a word
It does this by scanning for a space and shifting the
array elements at indices > where the space is
down by 1 as long as there is still a space there.
*/
for (int i = 0; i < 100; i++) {
while (word[i] == ' ') {
for (int j = i; j < 100; j++) {
word[j] = word[j + 1];
}
}
}
}
int isEmpty(char word[100]) {
// if a word consists of the empty character, it's empty
//otherwise, it isn't
if (word[0] == '\0') {
return 0;
}
return 1;
}
Je sais qu'il existe une bibliothèque qui peut traiter les chaînes de caractères, mais j'ai vraiment mais j'aimerais vraiment éviter d'avoir à l'utiliser. J'ai déjà parcouru tout ce chemin sans en avoir besoin, et je pense que le problème est en grande partie résolu, à l'exception d'une petite chose que je ne vois pas.
Je viens d'un milieu Java, et je suis nouveau en C, si cela explique l'erreur que j'ai commise.