Challenge 1

0 Marcus Sinclair · September 25, 2014
Brand new to this so apologies to all for sounding like a total noob. I am starting to write for the first challenge and running into difficulties already.

1. I assume for this challenge we are using only code from the tutorials up till this challenge?
2. Assuming the above is true I'm starting with a basic 4dig code so I had to use one of each. I tried some if else statement to test each digit before moving to the next but in my first if statement
"if (isupper)(pword[0]) || (islower)(pword[0]) || (isdigit)(pword[0]) || (pword[0]==$)"

I realised if I carry on like that for the rest of the tests it didn't matter what I entered as a password overall defeating the object of the challenge.

Need advise as to how to approach this..

Below is what I have so far..

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

int main()
{

char pword[5];
char $;

printf("Please enter a password. (must include an Upper case, digit, & $ sign)\n");
scanf(" %s",pword);



printf("Password Okay.");

return 0;
}

Not much I know lol.

My finished program.....
int main()
{
char a;
char b;
char c;
char d="$";
char e;
printf("Enter a password.\n");
printf("..Enter a Upper case letter:");


scanf(" %c",&a);
if (isalpha(a))
    if (isupper(a))
{printf("OK\n");}

printf("..Please enter a digit:");
scanf(" %c",&b);
if (isdigit(b))
 {printf("good!\n");}

 printf("..Please enter a '$' sign:");
scanf(" %c",&c);
if (c==d)
 {printf("Great!\n");}

 printf("..Please enter a random letter or digit:");
scanf(" %c",&e);
if ((isdigit(e))||(isalpha(e)))
{printf("Well done!\n\n\nCongratulations!\n...Your password %c%c%c%c is Okay.",a,b,c,e);}

else {printf("Please try again.");}



       return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · September 25, 2014
First of all if you have a string (char array) and you want to check the input char by char it's allways good to start with a loop.
e.g.:
for(int i = 0; i < strlen(pword); i++){
   if(isupper(pword)){
       capital_letter = true;
   }
   .
   .
   .
}

In this loop you make all your conditions dynamical for the character 


After the you looped through all your chars you can check if all conditions are statisfied:
if(capital_letter && digit && .....){
   printf("Password Okay.");
}
else{
   printf("Password NOT Okay.");
}

Btw.: use code tags please:angel:
0 Marcus Sinclair · October 4, 2014
How do I write the following so that it tests if a is upper case or not? Right now it just skips to print bad.


int main()
{
char a;

scanf(" %c",&a);
if (a==isupper)
{printf("good");}
else {printf("bad.");}



       return 0;
}
0 Marcus Sinclair · October 4, 2014
My Password challenge.

int main()
{
char a;
char b;
char c;
char d="$";
char e;
printf("Enter a password.\n");
printf("..Enter a Upper case letter:");


scanf(" %c",&a);
if (isalpha(a))
    if (isupper(a))
{printf("OK\n");}

printf("..Please enter a digit:");
scanf(" %c",&b);
if (isdigit(b))
 {printf("good!\n");}

 printf("..Please enter a '$' sign:");
scanf(" %c",&c);
if (c==d)
 {printf("Great!\n");}

 printf("..Please enter a random letter or digit:");
scanf(" %c",&e);
if ((isdigit(e))||(isalpha(e)))
{printf("Well done!\n\n\nCongratulations!\n...Your password %c%c%c%c is Okay.",a,b,c,e);}

else {printf("Please try again.");}



       return 0;
}
0 Marcus Sinclair · October 13, 2014
Trying this from scratch again.

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

int main()
{
char a;
char b;
char c;
char d="$";
char e;
printf("Enter a password.\n");
printf("..Enter a Upper case letter:");


scanf(" %c",&a);
if (isalpha(a))
{
    if (isupper(a)) printf("Well done %c is ok.",a);
}else {printf("Wrong try again.\n");scanf("%c",&a);}


return 0;
}

I would like to loop each section for example to keep requesting for an Upper case if they do not enter one. I do not know the proper way to state this in a while loop. In other words how do I sort the followings syntax out.
do {printf("Wrong try again.\n");scanf("%c",&a);}

while (!=isupper(a));
  • 1

C

124,384 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator