Hello,
I'm a newbie to C language so I need some help on comparing two strings to see if they're identical.
I have a set of characters in array1 (2D), and a character array2 (1D). Basically what I want to do is to compare to see if character in array2 match any of the character sets in array1, and then It returns 0 if there's a match or -1 if there insn't.
It would be easier if C supports string array, but since it doesn't, i'm a little bit stuck here, can someone please help?
In example below the sizes of the arrays are arbitray.
I'm a newbie to C language so I need some help on comparing two strings to see if they're identical.
I have a set of characters in array1 (2D), and a character array2 (1D). Basically what I want to do is to compare to see if character in array2 match any of the character sets in array1, and then It returns 0 if there's a match or -1 if there insn't.
It would be easier if C supports string array, but since it doesn't, i'm a little bit stuck here, can someone please help?
In example below the sizes of the arrays are arbitray.
const char array1[5][8] = {"add","subt","mult","div"};
char array2[100] = "adhowtocompare";
for (i = 0; i < 5; i++) {
if(array1[i][0] != array2[i]) {
continue;
for(j = 1; j < 8; j++) {
if(array1[i][j] != array2[j]) {
break;
}
// i'm not sure where to have a return statement
}
return -1;
}