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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top