MHB Checking if PassCode Contains a Digit

  • Thread starter Thread starter ksepe
  • Start date Start date
AI Thread 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
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top