I have wrote a program a little game of hangman to learn a little history to handle the strings. I think it's a fairly trivial exercise, and I wondered if people here had to give me advice for program improvement and syntax, because I did it with my little knowledge. I am sending the code that I used for my source program :
Code:
Stdio.h
/ *
Compares two strings and returns 1 if they are identical, otherwise 0
Takes 2 strings as input
Compare the strings, letters by Letters
* /
Compare HI int (char str1 [], char str2 [])
{Int i = 0, test = 1;
while (str1 [i] & & str2 [i] & & test)
if (str1 [i + +]! = ch2 [i + +]) test = 0;
return test;}
/ *
Returns 1 if the letter is contained in the word, otherwise returns 0
Takes as input a string and a letter
Compare the word letter by letter with the letter
* /
LetterTest int (char word [], char letter [])
{Int i = 0, test = 0;
while (word [i])
if (word [i + +] == letter [0]) test = 1;
return test;}
/ *
Displaying the incomplete word, returns 1 if the word is complete, otherwise 0
It sends word to find, and the string containing the characters
already found.
Compare the word letter by letter with the string containing the letters
found, displays the character if it is in this chain, displays
and put an underscore variable WIN 0 otherwise
* /
int display (char word [], char find [])
{Int i =- 1, win = 1;
while (word [i + +])
{If (LetterTest (find, & word [i]) == 1) printf ("% c", word [i]);
else {printf ("_") win = 0;}}
return win;
}
/ *
Main loop:
Request a word which must then guess
As the chain entry! = Not quit and Display
not return 1:
Request a character or string 'quit'
/ "\ No protection to limit other
channels possible bugs has predict / "\
If the letter belongs to the word, add in the chain
letters found
Increment the number of trials
Show Score when the main loop is finished
* /
main ()
{
char word [30], letter [2] = "", found [30] = "";
int trials = 0, n = 0;
printf ("\ n \ nEnter a word found \ n \ n") scanf ("% s", word); printf ("\ n \ n");
while ((Compare HI (letter, "quit")! = 1) & & (Display (word find)! = 1))
{
printf ("\ n \ n \ nEnter a letter or 'quit' to quit \ n");
scanf ("% s", letter); printf ("\ n \ n");
if (LetterTest (word, letter)) found [n + +] = letter [0];
trials + +;
}
printf ("\ n \ n% d tests \ n", tests);
return 0;
}
Bookmarks