So here I wrote this program which produces 2 sets of numbered words connected to them so later we can generate words from combining words from these sets (each set separately), every time we generate these two words it compares them if these words are same, if are it ends program.
strcmp is not working as it should and I have no idea why :...C can you help me please
I'm using code::blocks on Ubuntu 14.04 LTS and of course here is the code:
#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;
}
listA
and listB
should be initialize for strcat(listA[LISTSIZE - 1], listA[i - 1]);strcat(listB[LISTSIZE - 1], listB[i - 1]);
char listA[LISTSIZE][CHAINSIZE]={0}, listB[LISTSIZE][CHAINSIZE]={0};
also
scanf("%s", listA[j]);
...
scanf("%s", listB[j]);