I am trying to write a program in c using array pointers...
My goal is to print the error message if the header values are same..will get the header values via command line argument.
If I get the same header Values, ie)(GET and GET) or (HEAD and HEAD),it should print valid and for all the other cases,it should print Invalid.
The program which i have written prints Invalid for all the combinations .i don't understand where i'm making making mistake.
int main(int argc,char *argv[])
{
char *str1[4] = { "GET","HEAD","POST","OPTIONS"};
char *str2[2] = {NULL};
char *string = argv[1];
const char s[2] = ",";
char *token = "";
int i = 0,j = 0,k = 0,l = 0,m = 0;
token = strtok(string, s);
while( token != NULL )
{
if(i < 2)
{
str2[i] = token;
//printf( " %s\n", token );
printf("str2[%d]= %s\n",i,str2[i]);
}
i++;
token = strtok(NULL, s);
}
for(l = 0;l < 4;l++)
printf("str1[%d] = %s\n",l,str1[l]);
for( m = 0; m < 2;m++)
printf("str2[%d] = %s\n",m,str2[m]);
for(j = 0; j < 4;j++)
{
for(k =0; k < 2;k++)
{
if(str2[k] != str1[j])
printf("Invalid :: %s and %s\n",str2[k],str1[j]);
else
printf("Valid :: %s and %s\n",str2[k],str1[j]);
}
}
return 0;
}
compiling the code and getting output like this
bash-3.2$ gcc -o arraypointer arraypointer.c
bash-3.2$ ./arraypointer GET,HEAD
str2[0]= GET
str2[1]= HEAD
str1[0] = GET
str1[1] = HEAD
str1[2] = POST
str1[3] = OPTIONS
str2[0] = GET
str2[1] = HEAD
Invalid :: GET and GET
Invalid :: HEAD and GET
Invalid :: GET and HEAD
Invalid :: HEAD and HEAD
Invalid :: GET and POST
Invalid :: HEAD and POST
Invalid :: GET and OPTIONS
Invalid :: HEAD and OPTIONS
But the expected output is
If I get the same header Values((GET and GET) or (HEAD and HEAD),it should print valid.But its printing Invalid.
Could someone help me to get it resolved?