Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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!

   
0
huhulucy90
Asked:
huhulucy90
1 Solution
 
3rsrichardCommented:
int main(int argc, char* argv[])
{

if( strcmp(argv[1],"abcd")
{  // they match
} else {
// they don't match
};
0
 
huhulucy90Author Commented:
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!
0
 
Jan LouwerensSoftware EngineerCommented:
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.
0
 
Jan LouwerensSoftware EngineerCommented:
..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()
0
 
jpjpjpCommented:
note that strcmp will return 0 if the words are the same, so the if should be

if (! strcmp (..., ...))
{
  /* same words */
}
else
{
   /* diff */
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now