Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/Questions/Set hasDigit to true if the 3-character passCode contains a digit?
Next
Answered
amina42
  • 12
amina42
Asked: May 5, 20222022-05-05T05:34:06+00:00 2022-05-05T05:34:06+00:00In: c

Set hasDigit to true if the 3-character passCode contains a digit?

  • 12

How to set hasdigit to true if the 3-character passcode contains a digit. Below is my code

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

int main(void) {
bool hasDigit;
char passCode[50];

hasDigit = false;
strcpy(passCode, "abc");

/* Your solution goes here */

if (hasDigit) {
printf("Has a digit.\n");
}
else {
printf("Has no digit.\n");
}

return 0;
}

Give me a few effective suggestions to set hasdigit to true if the 3-character passcode contains a digit.

passcodeset hasdigit to true
  • 3 3 Answers
  • 135 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    xosinski
    2022-05-05T05:39:15+00:00Added an answer on May 5, 2022 at 5:39 am

    You should add the following loop

    for (int i=0; passCode[i]; i++)
        if (isdigit(passCode[i]))
            hasDigit = true;

    Your code after rewritten is

    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    #include <ctype.h>
    
    int main(void) {
    bool hasDigit;
    char passCode[50];
    
    hasDigit = false;
    strcpy(passCode, "abc");
    
    /* Add loop to set hasdigit to true if the 3-character passcode contains a digit*/
    for (int i=0; passCode[i]; i++)
    if (isdigit(passCode[i]))
    hasDigit = true;
    
    if (hasDigit) {
    printf("Has a digit.\n");
    }
    else {
    printf("Has no digit.\n");
    }
    
    return 0;
    }
    • 22
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. jazlyn84
    2022-05-09T13:43:52+00:00Added an answer on May 9, 2022 at 1:43 pm

    Suggestion command could be solved to your problem

    bool hasDigit = false;
    for (size_t i = 0; passCode[i]; ++i) {
    if (isdigit((unsigned char)passCode[i])) {
    hasDigit = true;
    break;
    }
    }
    

    Be aware it is important to note that isdigit (and every function) does not always return 1 so comparing it with true is not correct. Simply check to see whether it returns zero or non-zero. That’s the value that isdigit is stated to return.

    You’ll apply a similar loop for the second one and then do:

    for (size_t i = 0; passCode[i]; ++i) {
    if (isspace((unsigned char)passCode[i])) {
    passCode[i] = '_';
    }
    }
    • 22
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. 2022-05-09T13:46:34+00:00Added an answer on May 9, 2022 at 1:46 pm

    I have simple answer and it works for me

    if (isdigit(passCode.at(0)) || isdigit(passCode.at(1)) || isdigit(passCode.at(2)))
    {
    hasDigit = true;
    }
    • 20
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.