Challenge #1 - A little problem

0 Giorgi Aptsiauri · September 20, 2014
Okay, I have a little problem with challenge #1. I wrote the code but Special Characters is always +1.  The program works correctly only if I assign variable SpecialCharacters to -1 .
source code:

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

int main() {

int specialCharacters = 0;
int digits = 0;
int letters = 0;
int upperCases = 0;
int x;


int maxCharacters = 15;
char pass[15];

// a little prompt
printf("Please enter your password (max 15)\n");

do {
    if (strlen(pass) > maxCharacters){
    printf("Your password is too long. Try again below\n");
    }
    scanf(" %s", pass);


}while(strlen(pass) > maxCharacters);


printf("Your password is: %s\n", pass);




for(x = 0; x <= strlen(pass); x++){
    if( isalpha(pass[x]) ) {
        letters++;
        if( isupper(pass[x]) ){
            upperCases++;
        }
    }else{

    if( isdigit(pass[x]) ){
        digits++;
    } else{
    specialCharacters++;
    }
    }
}



printf("Digits: %d\n", digits);
printf("Letters: %d\n", letters);
printf("Uppercases: %d\n", upperCases);
printf("Special Characters: %d\n", specialCharacters);


int a;
scanf(" %d", &a);

return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Giorgi Aptsiauri · September 21, 2014
+1.  Thank you so much. I got it now.

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

int main() {

int specialCharacters = 0;
int digits = 0;
int letters = 0;
int upperCases = 0;
int x;


int maxCharacters = 15;
char pass[15];

// a little prompt
printf("Please enter your password (max 15)\n");

do {
    if (strlen(pass) > maxCharacters){
    printf("Your password is too long. Try again below\n");
    }
    scanf(" %s", pass);


}while(strlen(pass) > maxCharacters);


printf("Your password is: %s\n", pass);




for(x = 0; x < strlen(pass); x++){
    if( isalpha(pass[x]) ) {
        letters++;
        if( isupper(pass[x]) ){
            upperCases++;
        }
    }else{

    if( isdigit(pass[x]) ){
        digits++;
    } else{
    specialCharacters++;
    }
    }
}



printf("Digits: %d\n", digits);
printf("Letters: %d\n", letters);
printf("Uppercases: %d\n", upperCases);
printf("Special Characters: %d\n\n", specialCharacters);


int a;
scanf(" %d", &a);

return 0;
}

Now this code works perfectly <3
  • 1

C

107,332 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator