Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * FGL - roleplay.FutureGadgetLab.net posted by @alaestor
- * the following is a simple guess the password example
- * without using strcmp or other dedicated string functions.
- * The for() can be compacted to be on one line. Newlined for
- * the 79 char limit. Makes use of in-condition assignment.
- */
- #include <cstdio>
- bool AreStringsTheSame(const char *strA, const char *strB)
- { // only works with null terminated strings!
- bool cmp;
- for (
- int i = 0;
- (cmp = strA[i] == strB[i]) && (strA[i] != '\0' && strB[i] != '\0');
- i++
- );
- return cmp;
- }
- int main()
- {
- char buffer[80];
- const char *secret = "password";
- // if your password is password, you're a horrible person
- printf("\n\nEnter the password: ");
- if ( scanf("%79s", buffer) == EOF )
- return 0; // error: something went horribly wrong
- printf("\n\nYour answer of %s was ", buffer);
- if (AreStringsTheSame(buffer, secret))
- { // optimal for ternary but if{}else{} for clarity.
- puts("correct :D");
- }
- else
- {
- puts("incorrect :(");
- }
- char c; // clear stupid input buffer and wait for user
- while ((c = getchar()) != '\n' && c != EOF);
- puts("\n\nPress Enter to exit");
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement