MHB Checking if PassCode Contains a Digit

  • Thread starter Thread starter ksepe
  • Start date Start date
Click For Summary
The discussion centers on a C programming challenge to determine if a 3-character passCode contains a digit. The initial code attempts to set the boolean variable hasDigit to true if a digit is found, but it incorrectly uses the loop index `i` in the `isdigit` function instead of the actual character from the passCode string. The correct approach involves iterating through each character of the passCode and checking if it is a digit using `isdigit(passCode[i])`. If a digit is found, hasDigit should be set to true. The output should then reflect whether the passCode contains a digit or not. The thread also references an external resource for further guidance on similar programming problems.
ksepe
Messages
5
Reaction score
0
Set hasDigit to true if the 3-character passCode contains a digit. My attempt is bolded. It is not recognizing number's
Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

int main(void) {
   bool hasDigit = false;
   char passCode[50] = "";
   int valid = 0;

   strcpy(passCode, "abc");

[B]   /* Your solution goes here  */
   int i=0;
for (i=0; i<3; i++) {
   hasDigit=isdigit(i);
   if(hasDigit) {
      hasDigit=true;
   }
}[/B]
   if (hasDigit) {
      printf("Has a digit.\n");
   }
   else {
      printf("Has no digit.\n");
   }

   return 0;
}
 
Technology news on Phys.org
You may find this thread useful:

http://mathhelpboards.com/computer-science-58/c-determining-if-string-contains-any-numeric-digits-17748.html
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 4 ·
Replies
4
Views
10K
Replies
2
Views
51K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
14
Views
3K