0

If the user enters one the passwords below, I will ask him about secret question, but I don't do it using if else...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 20

int main(void) {
    char pass[SIZE];
    char originalPass[SIZE] = "abcDEF123=!)";
    printf("Password: ");
    fgets(pass, sizeof pass, stdin);
    printf("\n");
                      
    // if the user enters one the passwords bellow, I will ask him about secret Q
    // but I don't do it using if else...

    /* 
      bacDEF123=!)
      acbDEF123=!)
      abDcEF123=!)
      abcEDF123=!)
      abcDFE123=!)
      abcDE1F23=!)
      abcDEF213=!)
      abcDEF132=!)
      abcDEF12=3!)
      abcDEF123!=)
      abcDEF123=)!
    */
    return 0; 
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Bilal
  • 23
  • 3
  • 6
    To begin with, ***never ever*** use `gets`! It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that is has even been removed from the C language. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Dec 10 '21 at 07:49
  • Secondly, you seem to have forgotten that strings in C are really called ***null-terminated** strings*. For a string of 12 characters, you really need space for 13 to fit the terminator. – Some programmer dude Dec 10 '21 at 07:49
  • 2
    And please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). What is the problem you have? What have you tried? How did your attempt work or not work? Please [edit] your question to improve it, like actually asking a question. – Some programmer dude Dec 10 '21 at 07:51
  • Yes you're right...I have to consider the '\0' – Bilal Dec 10 '21 at 07:51
  • 1
    Please [edit] your question to include the *full* and *complete* assignment text, with all requirements and limitations. Copy-paste it, as text, and not rewrite it. – Some programmer dude Dec 10 '21 at 08:16

2 Answers2

2

You could enter all the passwords into an array and use a for (or any other loop) loop to iterate through the array and compare each element to the originalPass.

EDIT: Please dont use gets, use fgets instead

#define LENGTH   //number of strings 
char passwords[LENGTH][SIZE]= { //enter strings here }; 
int i =0; 
while(i<LENGTH)
{ 
  if( strcmp(passwords[i], originalPass) == 0) 
    { 
       // do something
    }
  i++; 
}
newt
  • 321
  • 2
  • 8
  • Can you show how you test the comparison result without an `if` statement? – chqrlie Dec 10 '21 at 08:12
  • @chqrlie Why should it be without _any_ `if`? The OP asks for a solution without a chain of `if`-`else`. -- Your solution is "correct" just by the word. A `while` uses a condition and is therefore some kind of `if`. – the busybee Dec 10 '21 at 10:12
  • 1
    There is no need to define a `LENGTH`. Use `const char *passwords[] = { /* strings */ };` and use the condition `i < sizeof passwords / sizeof passwords[0]`. -- Additionally, a loop with an initial statement `i = 0;`, a condition `i < ...`, and a loop statement `i++;` is perfectly expressed by a `for` loop. Keep things together that belong to each other. – the busybee Dec 10 '21 at 10:15
0

You can use a fuzzy comparison function:

/* return 0 if the strings compare equal,
          1 if they compare equal with a single swap
          -1 otherwise
*/
int fullycmp(const char *s1, const char *s2) {
    int swap = 0;
    size_t i = 0;
    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);

    if (len1 != len2)
        return -1;

    while (i < len && s1[i] == s2[i])
        i++;

    if (i < len - 1 && s1[i] == s2[i + 1] && s1[i + 1] == s2[i]) {
        swap = 1;
        i += 2;
    }
    while (i < len && s1[i] == s2[i])
        i++;

    return i == len ? swap : -1;
}

If you really must not use if or else, you can replace the if statements above with while statements:

int fullycmp(const char *s1, const char *s2) {
    int swap = 0;
    size_t i = 0;
    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);

    while (len1 != len2)
        return -1;

    while (i < len && s1[i] == s2[i])
        i++;

    while (i < len - 1 && s1[i] == s2[i + 1] && s1[i + 1] == s2[i]) {
        swap = 1;
        i += 2;
        break;
    }
    while (i < len && s1[i] == s2[i])
        i++;

    return i == len ? swap : -1;
}

Also make sure you strip the newline with:

pass[strcspn(pass, "\n")] = '\0'; // strip the trailing newline
chqrlie
  • 131,814
  • 10
  • 121
  • 189