Replace any space ' ' with '_' in 2-character string passCode. Sample output for the given program:
My program needs to do the above. I believe most of my code is correct but it keeps failing the final Test as you can see from the attached picture. I would apperciate it if you could tell me what I need to alter to pass all the tests.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char passCode[3];
strcpy(passCode, "1 ");
if (isspace(passCode[0]))
{
passCode[0]='_';
}
else if (isspace(passCode[1]))
{
passCode[1]='_';
}
else if (isspace(passCode[2]))
{
passCode[2]='_';
}
else if (isspace(passCode[3]))
{
passCode[3]='_';
}
printf("%s ", passCode);
return 0;
}
Solution:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char passCode[3];
strcpy(passCode, "1 ");
for (int i = 0; i < strlen(passCode); i++) {
if (isspace(passCode[i])) {
passCode[i] = '_';
}
}
printf("%s", passCode);
return 0;
}
0 Comments