Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C programming Question on Characters

C programming Question on Characters

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 14065453
    wrote on last edited by
    #1

    /*Write a programme to check a password if it has an uppercase letter, a digit and a special symbol. (I have provided what I have done and I am not getting the output as required. Need explanation on where I messed it up.)*/ #include #include #include int main() { int i; int tuna; do{ printf("Enter a password:%c \n", tuna); scanf(" %c", &tuna); } while( tuna != (isupper(tuna), isalpha(tuna), isdigit(tuna)) ); return 0; }

    D J 2 Replies Last reply
    0
    • U User 14065453

      /*Write a programme to check a password if it has an uppercase letter, a digit and a special symbol. (I have provided what I have done and I am not getting the output as required. Need explanation on where I messed it up.)*/ #include #include #include int main() { int i; int tuna; do{ printf("Enter a password:%c \n", tuna); scanf(" %c", &tuna); } while( tuna != (isupper(tuna), isalpha(tuna), isdigit(tuna)) ); return 0; }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      I see several things wrong with your approach: 1) The first time through the loop, tuna is uninitialized so the printf() statement is going to print an incorrect character, 2) The scanf() statement is expecting to receive a char (%c) but you are giving it the address of an int, 3) The isupper(), isalpha(), and isdigit() functions return a non-zero value if the parameter is an uppercase alphabetic letter, and zero otherwise. You are then comparing this to tuna. While syntactically correct, it is nonsensical, 4) If your password is to hold numbers, letters, and symbols, the password type should not be an int, but should probably be a string. Consider something like:

      int reqd = 0;
      char pwd[32];
      printf("Enter a password: ");
      scanf("%s", pwd);
      for (int x = 0; x < strlen(pwd); x++)
      {
      if (isupper(pwd[x])) // this does not check for special characters
      reqd |= 0x1;
      else if (isalpha(pwd[x]))
      reqd |= 0x2;
      else if (isdigit(pwd[x]))
      reqd |= 0x4;
      }

      if (reqd == 0x07)
      printf("Requirements have been met.\n");
      else
      printf("Requirements have NOT been met.\n");

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      U 1 Reply Last reply
      0
      • D David Crow

        I see several things wrong with your approach: 1) The first time through the loop, tuna is uninitialized so the printf() statement is going to print an incorrect character, 2) The scanf() statement is expecting to receive a char (%c) but you are giving it the address of an int, 3) The isupper(), isalpha(), and isdigit() functions return a non-zero value if the parameter is an uppercase alphabetic letter, and zero otherwise. You are then comparing this to tuna. While syntactically correct, it is nonsensical, 4) If your password is to hold numbers, letters, and symbols, the password type should not be an int, but should probably be a string. Consider something like:

        int reqd = 0;
        char pwd[32];
        printf("Enter a password: ");
        scanf("%s", pwd);
        for (int x = 0; x < strlen(pwd); x++)
        {
        if (isupper(pwd[x])) // this does not check for special characters
        reqd |= 0x1;
        else if (isalpha(pwd[x]))
        reqd |= 0x2;
        else if (isdigit(pwd[x]))
        reqd |= 0x4;
        }

        if (reqd == 0x07)
        printf("Requirements have been met.\n");
        else
        printf("Requirements have NOT been met.\n");

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        U Offline
        U Offline
        User 14065453
        wrote on last edited by
        #3

        I didn't get the logic behind "reqd" in the if else part. Can you please explain?

        J D 2 Replies Last reply
        0
        • U User 14065453

          /*Write a programme to check a password if it has an uppercase letter, a digit and a special symbol. (I have provided what I have done and I am not getting the output as required. Need explanation on where I messed it up.)*/ #include #include #include int main() { int i; int tuna; do{ printf("Enter a password:%c \n", tuna); scanf(" %c", &tuna); } while( tuna != (isupper(tuna), isalpha(tuna), isdigit(tuna)) ); return 0; }

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Your code is entering a character (singular) then checking it. You need to enter a word (multiple characters) and THEN check that entire word for one character of each type.

          1 Reply Last reply
          0
          • U User 14065453

            I didn't get the logic behind "reqd" in the if else part. Can you please explain?

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            I presume this is for a class and I suspect you have not been taught bit manipulation. Unless you learn about it yourself, you probably do not want to hand in a solution that uses it.

            D 1 Reply Last reply
            0
            • J jschell

              I presume this is for a class and I suspect you have not been taught bit manipulation. Unless you learn about it yourself, you probably do not want to hand in a solution that uses it.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              jschell wrote:

              Unless you learn about it yourself, you probably do not want to hand in a solution that uses it.

              Good point. I didn't think through my offering well enough.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              1 Reply Last reply
              0
              • U User 14065453

                I didn't get the logic behind "reqd" in the if else part. Can you please explain?

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                You could (possibly) simplify to something like:

                int upper = 0;
                int alpha = 0;
                int digit = 0;

                ...
                if (isupper(pwd[x])) // this does not check for special characters
                upper = 1;
                else if (isalpha(pwd[x]))
                alpha = 1;
                else if (isdigit(pwd[x]))
                digit = 1;

                ...
                if (upper == 1 && alpha == 1 && digit == 1)
                printf("Requirements have been met.\n");
                else
                printf("Requirements have NOT been met.\n");

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups