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

#define HUNDRED 100   

int main()
{
    char str[HUNDRED];
    char forbiddenChars[] = "!@#$%^&*()+= ";
    scanf("%s",str);
    for(i=0;str[i];i++)
    {
        if(str[i] == newStr[]) //this if is supposed to check if they have the same char
        {
            //prints illegal password
        }
    }
}`

I was asked to write a code that checks if a password is "good", "good" means the password have no chars like !@#$%^&*()+= and does not have any same chars that go together, for example butter is bad because it has double t. also it has no spaces.

So I thought that I can make a string that contain all the illegal chars and then check if they both have some chars that are the same, but I have no idea how to do this. Please help me

Tal Sokolinsky
  • 88
  • 1
  • 14
  • Write code first. Show it to us. – cadaniluk Jan 06 '16 at 15:12
  • 1
    I have no idea how to do this – Tal Sokolinsky Jan 06 '16 at 15:13
  • Start with main, maybe add some includes, like stdio.h. – matt Jan 06 '16 at 15:14
  • You can go further: write the main func and a function check_password that always answers "good" for example. In short: go further as far as you can before asking. – mikedu95 Jan 06 '16 at 15:16
  • This thread might be of some use to you, OP: http://stackoverflow.com/a/1071555/2694851 – bazeblackwood Jan 06 '16 at 15:18
  • You can also go to the google. Try entering words like " C character string tutorial" and see what an astonishing amount of ready-to-use advice is out there. (The very first hit seemed good enough to me.) In fact, judging by the level of ignorance you exhibit, you may start with " C tutorial" proper. (I didn't check the results on that but suppose the first entry should be good enough as well.) – Peter - Reinstate Monica Jan 06 '16 at 15:18
  • here guys I don't know what to do next – Tal Sokolinsky Jan 06 '16 at 15:19
  • You know, google is a bit awkward, with the text interface and all. I just saw that there are youtube videos providing C tutorials. The homeopathic information density of about 1e-8 compared to text may be more manageable – Peter - Reinstate Monica Jan 06 '16 at 15:22
  • [Regular Expression for Password Validation C#](http://stackoverflow.com/questions/21760692/regular-expression-for-password-validation-c-sharp) – caot Jan 06 '16 at 15:25
  • This code example is actually not bad. You'll need a second loop *within* the first one: each single password char must be checked against each of the forbidden chars, i.e. for each `i` you have to have a `j` or whatever looping over all forbidden chars (would that be `newStr`? Then it is badly named). – Peter - Reinstate Monica Jan 06 '16 at 15:26

2 Answers2

0

There are a few ways to do this.

You can loop through the string and for each character check if it's one of the "bad" characters.

Alternatively, you can use regular expressions. They are not part of ANSI C, but are part of POSIX. I've linked a few resources about them below. If you want examples or more information about regex, google will be able to help you out quite a bit.

Manual page for C regex: http://pubs.opengroup.org/onlinepubs/009695399/functions/regcomp.html

Official GNU page on regex: http://www.gnu.org/software/libc/manual/html_node/Regular-Expressions.html

9y7h0n
  • 326
  • 1
  • 8
0

You are right to store all the illegal characters in a separate string.
So why you didn't do it?

newStr[] = "!@#$%^&*()+=";

Now for each character of newStr check whether it exists in str or not.
Hint - nested loops

return false on the very first occurrence of an illegal char.

Also simultaneously you should check if current character == previous character. If yes then return false.

Write and edit your code. If you get stuck we'll help you.

A furher hint:

int strLen = strlen(str);
int newStrLen = strlen(newStr);

for(i = 0; i < newStrLen; i++)
    for(j = 0; j < strLen; j++)
        if(newStr[i] == str[j])
        {
            printf("Bad Password");
            return 1;
        }  

Now add the logic to identify repetitive chars.

rootkea
  • 1,474
  • 2
  • 12
  • 32