Link to home
Start Free TrialLog in
Avatar of huhulucy90
huhulucy90

asked on

compare two word?

if I want to input a 4-letter word on the commond line,
and then compare it with "abcd", what should I do?
I mean, how to transfer this input word to a varible
in the beginning, like this way: word[4] = atoi(argv[1]); ?

thank you very much!

   
ASKER CERTIFIED SOLUTION
Avatar of 3rsrichard
3rsrichard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of huhulucy90
huhulucy90

ASKER

actually, it is a guessing question,
how can I get the total number of guesses if
I want to find the word that is inputed on the
commond line.
thanks!
Avatar of Jan Louwerens
sounds like a school assignment to me, so I won't give any code. But basically, you want to use a count variable, initialized to 0. Every time the user inputs a word, increment the count variable. Keep doing this until the strings match (as shown in 3rsrichard's comment). Then the count variable contains the number of guesses.
..and besides strcmp(), there's also strncmp(s1, s2, n), where it will only compare the 1st n characters of the strings. Bot of these are case sensitive comparisons. For a case insensitive compare, you can use strcasecmp() or strncasecmp()
note that strcmp will return 0 if the words are the same, so the if should be

if (! strcmp (..., ...))
{
  /* same words */
}
else
{
   /* diff */
}