因此,我在这里编写了此程序,该程序会生成2组与它们相连的带编号的单词,以便以后我们可以通过组合这些组中的单词(每个组分别)来生成单词,每次生成这两个单词时,如果这些单词相同,就会对其进行比较,如果结束程序。

strcmp无法正常工作,我也不知道为什么:... C
你能帮我吗

我在Ubuntu 14.04 LTS上使用代码:: blocks
当然是以下代码:

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

#define LISTSIZE 256
#define CHAINSIZE 64

int main()
{
char listA[LISTSIZE][CHAINSIZE], listB[LISTSIZE][CHAINSIZE]; //i know malloc would be nice here but it's not really important here
int j = 0, i = 0, n;
printf("Podaj wartość indeksu i: ");
scanf("%d", &i);

for(j = 0 ; j < i; j++)
{
    printf("Podaj łańcuch do listy A: "); //enter chain to list A
    scanf("%s", &listA[j]);

}
for( j = 0; j < i; j++)
{
    printf("Podaj łańcuch do listy B: "); //enter chain to list B
    scanf("%s", &listB[j]);
}
printf("Ile chesz podać indeksów? "); //how much indexes do you like to choose
scanf("%d", &n);
for (j = 0; j < n; j++)
{
    printf("\nWpisz index", j); //enter index
    scanf("%d", &i);
    strcat(listA[LISTSIZE - 1], listA[i - 1]);
    strcat(listB[LISTSIZE - 1], listB[i - 1]);
    printf("\nslowo A: %s", listA[LISTSIZE - 1]);
    printf("\nslowo B: %s", listB[LISTSIZE - 1]);
    printf("\n %d", strcmp(listA[LISTSIZE - 1], listB[LISTSIZE - 1])); //just to check
// here i have a problem
    if(strcmp(listA[LISTSIZE - 1], listB[LISTSIZE - 1]) == 0)
    {
        printf("\tRozwiązanie zostało znalezione!\n");
        return 0;
    }
}
printf("Nie znaleziono rozwiązania"); //no solution was found
return 0;
}

最佳答案

listAlistB应该为strcat(listA[LISTSIZE - 1], listA[i - 1]);strcat(listB[LISTSIZE - 1], listB[i - 1]);初始化

char listA[LISTSIZE][CHAINSIZE]={0}, listB[LISTSIZE][CHAINSIZE]={0};




scanf("%s", listA[j]);
...
scanf("%s", listB[j]);

关于c - strcmp在c中返回相同的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27912095/

10-11 16:39