Home > database >  Please, how can I find an algorithm which checks for inverting 2 consecutive characters
Please, how can I find an algorithm which checks for inverting 2 consecutive characters

Time:12-15

Please how can I check for the second condition (using an algorithm instead because I'm expecting other passwords, it means that I need to check for all of them, and basically it's impossible to know the passwords which will be entered, so I need an algorithm (inverting 2 consecutive characters)which does this task( for instance passwords might: (abcdefg), (hhgfts543),(GFYS!-rii)).

#include <stdio.h>
#include <string.h>
int main (void) {
char pass[50];
char validPass[50]="abc123";
printf("Your password: ");
scanf("%s", pass); // the expected password doesn't contain a space
if (strcmp(pass, validPas)==0) {
    printf("Password match...!");
}
else if (strcmp(pass, "bac123")==0 ||
         strcmp(pass, "acb123")==0 ||
         strcmp(pass, "ab1c23")==0 ||
         strcmp(pass, "abc213")==0 ||
         strcmp(pass, "abc123")==0 ||) {

  //An algorithm which checks for inverting 2 consecutive characters

printf("CLOSE TO GUESSINGING THE PASSWORD !");
}
else 
printf("WRONG PASSWORD !");
return 0; 
}

CodePudding user response:

You could use a method like this:

    // ALT: declare variable as false
    for (int i = 0; i<s.Length-1; i  ) /*loop through the characters except the last 
                                       (we want to swap each character with the next one)*/ 
    {
        if (s.Substring(0, i) /*unchanged first part*/ 
             s[i   1]   s[i] /*swapped characters*/ 
             s.Substring(i   2, s.Length - i - 2) /*rest of the string*/ 
           == test) 
        {
            return true; // ALT: variable to true
        }
    }
    return false; // remove in ALT

Sorry for not using c. The point should come across.
You could alternatively declare a first variable as false and only change it to true if the condition is met.
(marked as "ALT")
I hope you understand my intention and are able to use my input for the solution of your problem.

  •  Tags:  
  • c
  • Related