2

1.

Set hasDigit to true if the 3-character passCode contains a digit.

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

int main(void) {
   bool hasDigit;
   char passCode[50];

   hasDigit = false;
   strcpy(passCode, "abc");

   /* Your solution goes here  */

   if (hasDigit) {
      printf("Has a digit.\n");
   }
   else {
      printf("Has no digit.\n");
   }

   return 0;
}

What I tried (in place of /* Your solution goes here */ is:

if (isdigit(passCode) == true) {
    hasDigit = true;
}
else {
    hasDigit = false;
}

when testing

abc

it works, but when testing

a 5

it doesnt work.

2.

Replace any space ' ' with '_' in 2-character string passCode. Sample output for the given program:

1_

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void) {
char passCode[3];

   strcpy(passCode, "1 ");

   /* Your solution goes here  */

   printf("%s\n", passCode);
   return 0;
}

What I put in place of /* Your solution goes here */ is:

   if (isspace(passCode) == true) {
      passCode = '_';
   }

And it fails to compile.

Thanks for all help.

Michael Bonnet
  • 111
  • 2
  • 11
  • 3
    `isdigit(...)` takes a character but you're passing an array. Try looping over each character and passing `passCode[i]` – cleblanc Aug 23 '18 at 16:44
  • isdigit(passCode[i]) causes the compiler to tell me: In file included from main.c:4:0: main.c: In function ‘main’: main.c:13:25: error: ‘i’ undeclared (first use in this function) if (isdigit(passCode[i]) == true) { ^ main.c:13:25: note: each undeclared identifier is reported only once for each function it appears in – Michael Bonnet Aug 23 '18 at 16:46
  • 1
    also there is no need for `== true` if something is true the `if` will detect that – Martin Beckett Aug 23 '18 at 16:48
  • @SOMEK you will need to declare `int i;` and initialize it creating a `for(int i=0; i<3; i++)` loop to check each character in the array – cleblanc Aug 23 '18 at 16:48
  • Look at loop control in your text or tutorial. It's basic C (and just about any other language), and you'll do it for petty much the rest of your engineering career. The error is obvious: you didn't declare your loop-control variable `i` (if you even coded a loop at all; we don't know). – WhozCraig Aug 23 '18 at 16:49
  • @cleblanc I made it work with multiple else if statements for each index. Can you elaborate on how the for loop with i works? – Michael Bonnet Aug 23 '18 at 16:51

2 Answers2

3

Here's how you would use a for loop;

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

int main(void) {
   bool hasDigit;
   char passCode[50];

   hasDigit = false;
   strcpy(passCode, "abc");

   /* Your solution goes here  */
   for (int i=0; passCode[i]; i++)
       if (isdigit(passCode[i]))
           hasDigit = true;

   if (hasDigit) {
      printf("Has a digit.\n");
   }
   else {
      printf("Has no digit.\n");
   }

   return 0;
}
cleblanc
  • 3,678
  • 1
  • 13
  • 16
  • Thank you for your help, this solution worked. How do the i=0 and i++ parts of the for loop affect how it works? – Michael Bonnet Aug 23 '18 at 17:02
  • The structure of a `for` loop is like this `for ( initial_condition; terminating_condition; post_action ) { body }` – cleblanc Aug 23 '18 at 17:14
2

isdigit function takes an int as argument, not a char *. So you can't pass passCode. You'll have to loop over passCode and test each of the characters in passCode using isdigit.

For example:

bool hasDigit = false;

for (size_t i = 0; passCode[i]; ++i) {
    if (isdigit((unsigned char)passCode[i])) {
        hasDigit = true;
        break;
    }
}

...

Note that isdigit (and all <ctype> functions) doesn't necessarily return 1, so comparing with true is incorrect. Just check if it returns 0 or non-zero - that's what isdigit is documented to return.

You'll use a similar loop for the second problem and do:

for (size_t i = 0; passCode[i]; ++i) {
   if (isspace((unsigned char)passCode[i])) {
      passCode[i] = '_';
   }
}
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thank you, this solution worked. What is the purpose of "size_t", and why does unsigned char have to be specified, and how does specifying it change the program? – Michael Bonnet Aug 23 '18 at 17:05
  • 1
    1. https://stackoverflow.com/q/3174850/1275169 and 2. https://stackoverflow.com/q/21805674/1275169 answer both of your questions. – P.P Aug 23 '18 at 17:08
  • 1
    This good answer provides more information than the OP was perhaps asking. – Weather Vane Aug 23 '18 at 17:10
  • @WeatherVane I'm trying to learn as much as I can, so more info is awesome! – Michael Bonnet Aug 23 '18 at 17:13