int main(void)
    int i;
    char* kw = get_string(Enter keyword: \n");
    char* plntext = get_string(Enter plaintext: \n);
    for(i = 0;i < strlen(kw); i++)
        printf("%s, ", kw[i]);
        printf("%s\n", plntext};
        int comp = strcmp(&kw[i], &plntext[i]};
        printf("Comparision is %d\n", comp);
  • This code is full of compile bugs and typos. Are you sure that you copied working code? – Cliff B Apr 27 '18 at 19:49

Notwithstanding all of the bugs in the code above, here's the answer. strcmp() is comparing strings, not chars, starting at the indicated letter on each pass. Specifically, as soon as it finds mismatched chars, it'll report back a result. Now, if you're looping through the chars, the first char of both strings, d & b, produce a result of 2.

However, on the next loop, it is comparing the strings "ave" and "aca". Since the a's match, strcmp goes on to compare v and c, resulting in 19.

Maybe strcmp isn't what you want to do here. If you're comparing chars, == will work fine.

Special thanks to @curiouskiwi for reminding me that strcmp compares STRINGS!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .