我想制作一个程序来读取两个文件并将常用单词作为2-gram。我写了下面的代码。常见单词检查循环不起作用
这里是节点
struct node {
char *string;
struct node *next;
};
这里是检查循环
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;
}
,当我试图打印matched2链表。它给我21 文档这是第一个文件的最后两个单词甚至不常见。我认为strcmp函数有问题。
我之前问过类似的问题,但他们不一样。
这里是我如何打印matched2链表。
while(matched2 != NULL){
printf("%s\n", matched2->string);
matched2 = matched2->next;
}
这里是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;
你真的必须初始化你的字符串缓冲区,否则他们会充满垃圾数据。 – tadman
@tadman我指定他们什么值? –
根据您的偏好,您总是可以用'memset'或'bzero'将它归零。初始化为零字节也是一种选择。 C字符串是NULL终止的,因此您需要在使用任何标准字符串函数之前终止它们。 – tadman