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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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