Je veux faire un programme qui lit deux fichiers et me donne le mot commun de 2 grammes. J'ai écrit le code ci-dessous.Les mots courants vérifier la boucle ne fonctionne pas
ici est le nœud
struct node {
char *string;
struct node *next;
};
est ici la boucle de contrôle
struct node *sw1, *sw2, *sw1_head;
//first 1 and first2 is the head of linked lists that holds text's each word seperatly. i created before.
first1 = first1_head; // _ head is the same as first1 and first2
first2 = first2_head;
//sw1 and sw2 are the pointers that holds always second words.
sw2 = first2->next;
sw1 = first1->next;
sw1_head = sw1;
//these chars are used to concat two words
char destination1[50];
char destination2[50];
while(sw2 != NULL){
strcpy(destination2,first2->string);
strcat(destination2,sw2->string);
while(sw1 != NULL){
strcpy(destination1,first1->string);
strcat(destination1,sw1->string);
// printf("%s\n", destination1);
if(strcmp(destination2, destination1) == 0) {
insert(&matched2, destination1);//matched holds common words
}
sw1 = sw1->next;
first1 = first1->next;
}
sw1 = sw1_head;//sets both sw1 and first1 to their past positions.
first1 = first1_head;
sw2 = sw2->next;
first2 = first2->next;
}
Lorsque j'ai essayé d'imprimer le matched2 chaînée. Il me donne 21 document qui est les deux derniers mots du premier fichier qui n'est même pas commun. Je pense qu'il y a quelque chose de mal dans fonction strcmp.
J'ai déjà posé une question similaire mais elles ne sont pas identiques.
Voici comment j'imprime matched2 linked-list.
while(matched2 != NULL){
printf("%s\n", matched2->string);
matched2 = matched2->next;
}
est ici la méthode insert
void insert(struct node **new_node, char* new_data){
/* allocate node */
struct node *ptr1 =(struct node*) malloc(sizeof(struct node));
/* put in the data */
ptr1->string = new_data;
ptr1->next = NULL;
if(new_node == NULL){
*new_node = ptr1; return;
}
ptr1->next = *new_node;
*new_node = ptr1;
Vous devez vraiment initialiser vos tampons de chaîne ou ils seront pleins de données de vidage. – tadman
@tadman quelles valeurs dois-je nommer à eux? –
Vous pouvez toujours le mettre à zéro avec 'memset' ou' bzero' selon vos préférences. L'initialisation en tant que zéro octet est également une option. Les chaînes C sont terminées par NULL, vous devez donc les terminer avant d'utiliser l'une des fonctions de chaîne standard. – tadman