Counting specific characters in a String

+1 william willette · September 29, 2014
I am working on a modified "Challenge1" .

I am trying to learn how to count any specific character i have in a string (password)
and store the amount in another integer variable.
In counting the number of $ signs, my program will not accurately count $ signs side by side.
A pass word of: MyCats$eatbear$$,   will only count 2 '$'s for instance.

I think i read that C++ has a way to do this easily, but hey im a beginner' and learning C with Bucky!. 
After 2.5+ hrs of trying things to count '$', i though it time to come here. 
For kicks here is my WIP 'Challenge1 exploring counting, load it up and type in a password.
Been using cplusplus.com's reference for learning about different library functions. 
-Will

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

int main()
{
    // $ sign pointer
    int * dollarSign; // Got money in your password?
    char z[]= "$";
    int y = 0; // $ counter
    int digit = 0; // numbers counted
    int upper = 0; // upper case counted
    int x = 0;     // variable to count-tool
    char passWord[200];
    int stringLength = 0;

    printf("Enter a password: ");
    scanf("%s",passWord);
    //string length section
    stringLength = (strlen(passWord));
    printf("\n\nYour password is: %d",stringLength);
    printf(" characters long.");

    //Test for upper case letters
    for(x;x <= stringLength; x++){
        if(isupper(passWord[x])){
            upper++;
        }else{
            if(isdigit(passWord[x])){
                digit++;
            }
            }
        }

    //test for the dollar sign
    /* Cant count accurate number of $'s yet if there two in a row...*/
    /*
    dollarSign=strchr(passWord, '$');

    while (dollarSign != NULL ){
        dollarSign=strchr(dollarSign+1,'$');
        z++;

    }
    */

    dollarSign = strpbrk(passWord,z);
    while (dollarSign != NULL ){
        dollarSign = strpbrk(dollarSign+1,z);
        y++;
    }

    printf("\n\nYou have at least: %d dollar sign(s) present",y);

    printf("\n\nYou have: %d uppercase letters",upper);




    printf("\n\n\n\n\n\n");
    return 0;

}




Post a Reply

Replies

Oldest  Newest  Rating
0 william willette · September 29, 2014
Looking at other folks code, i found an elegant and simple solution to count a specific character in a string.
Here's a demo of how to count a specific character.  
Int X = 0;  // for loop' closer
int y = 0; //Place to store how many times '$' appears in our password' string
int stringLength = 20; // number of characters in our string.
char passWord[] = Cats$$eatBabies$$$; // hypothetical password entered

// int 'x' is already defined as zero, but have to set it to zero again if you changed
//its value.
// Here is the loop that works to count our specific character.
for(x=0; x<= stringLength; x++){
        if(passWord[x] == '$'){
            y++;
        }


Here is my code that counts how long the password is, how many uppercase letters and how many times you enter the '$'.


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

int main()
{
    // $ sign pointer
    int * dollarSign; // Got money in your password?
    int y = 0; // $ counter
    int digit = 0; // numbers counted
    int upper = 0; // upper case counted
    int x = 0;     // variable to count-tool
    char passWord[200];
    int stringLength = 0;

    printf("Enter a password: ");
    scanf("%s",passWord);
    //string length section
    stringLength = (strlen(passWord));
    printf("\n\nYour password is: %d",stringLength);
    printf(" characters long.");

    //Test for upper case letters
    for(x;x <= stringLength; x++){
        if(isupper(passWord[x])){
            upper++;
        }else{
            if(isdigit(passWord[x])){
                digit++;
            }
            }
        }

    // New code to count the $ sign! Thanks * Jojo Kim.
    // I have alot to learn.
    for(x=0; x<= stringLength;x++){
        if(passWord[x] == '$'){
            y++;
        }
    }





    printf("\n\nYou have at least: %d dollar sign(s) present",y);

    printf("\n\nYou have: %d uppercase letters",upper);




    printf("\n\n\n\n\n\n");
    return 0;

}
  • 1

C

106,966 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator