Checking if PassCode Contains a Digit

  • Context: MHB 
  • Thread starter Thread starter ksepe
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on implementing a check to determine if a 3-character passCode contains a digit using C programming. The initial code provided incorrectly uses the loop index instead of the character from the passCode string to check for digits. The correct approach involves iterating through each character in the passCode and applying the isdigit() function to each character. The final solution should set hasDigit to true if any character in the passCode is a digit.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with string manipulation functions in C, such as strcpy()
  • Knowledge of the isdigit() function from ctype.h
  • Basic understanding of boolean logic in programming
NEXT STEPS
  • Learn how to properly iterate through strings in C
  • Research the use of the isdigit() function in C programming
  • Explore debugging techniques for C code to identify logical errors
  • Study best practices for validating user input in C applications
USEFUL FOR

C programmers, software developers, and anyone interested in string manipulation and validation techniques in C.

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
 

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