Uppercase, lowercase, number in string
#1

Hey guys,

Is it possible to check if a string contains a uppercase letter, lowercase letter, and a number? If so, how? I know that you can do it with php with preg_match, but i dont recall any function like preg_match in pawn. Anyone mind helping me out?

Ty

~Wesley
Reply
#2

I've written something for ya:
pawn Code:
stock checker(string[]) {
    new valid = true;
    new patterns[3][] = {
        { "ABCDEFGHIJKLMNOPRSTUWXYZ" },
        { "abcdefghijklmnoprstuwxyz" },
        { "0123456789" }
    }
    for(new i = 0; i < sizeof(patterns); i++) {
        for(new j = 0; j < strlen(patterns[i]); j++) {
            //This is due to something weird with patterns[i][j]
            new cchar = patterns[i][j]; //We need char
            new sub[2];
            format(sub, sizeof(sub), "%s", cchar); //And string again
            if(-1 != strfind(string, sub)) break; //Found it!
            else {
                //Check if last run, if yes, jump out of here to label
                if(j == (strlen(patterns[i])-1)) {
                    valid = false;
                    goto finish_now;
                }
            }
        }
    }
    finish_now:
    return valid;
}
You can change patterns to some other if you want

Example usage:
pawn Code:
new str[5];
str = ((checker("I like 9 pancakes")) ? ("Yup") : ("Nope"));
printf("%s", str);
str = ((checker("I like pancakes")) ? ("Yup") : ("Nope"));
printf("%s", str);
str = ((checker("i like pancakes")) ? ("Yup") : ("Nope"));
printf("%s", str);
Result: Yup, Nope, Nope

(Goto only for sake of getting out of double loop without additional variables)
Reply
#3

Quote:
Originally Posted by Misiur
View Post
I've written something for ya:
pawn Code:
stock checker(string[]) {
    new valid = true;
    new patterns[3][] = {
        { "ABCDEFGHIJKLMNOPRSTUWXYZ" },
        { "abcdefghijklmnoprstuwxyz" },
        { "0123456789" }
    }
    for(new i = 0; i < sizeof(patterns); i++) {
        for(new j = 0; j < strlen(patterns[i]); j++) {
            //This is due to something weird with patterns[i][j]
            new cchar = patterns[i][j]; //We need char
            new sub[2];
            format(sub, sizeof(sub), "%s", cchar); //And string again
            if(-1 != strfind(string, sub)) break; //Found it!
            else {
                //Check if last run, if yes, jump out of here to label
                if(j == (strlen(patterns[i])-1)) {
                    valid = false;
                    goto finish_now;
                }
            }
        }
    }
    finish_now:
    return valid;
}
You can change patterns to some other if you want

Example usage:
pawn Code:
new str[5];
str = ((checker("I like 9 pancakes")) ? ("Yup") : ("Nope"));
printf("%s", str);
str = ((checker("I like pancakes")) ? ("Yup") : ("Nope"));
printf("%s", str);
str = ((checker("i like pancakes")) ? ("Yup") : ("Nope"));
printf("%s", str);
Result: Yup, Nope, Nope

(Goto only for sake of getting out of double loop without additional variables)
Brother if you can explain the code to me a little more , ill appreciate a lot. Im bad at strings as i bunked every class at my college.

I mean can you change it a bit to return 1 and o if true or false. If the string contains an Upper Case, Lower Case and a Number , it will return true(1) else false(0).

And simplify it a bit with elaboration?

(Rep+6 Added)
Reply
#4

made it quickly, it should work well.
pawn Code:
stock check(string[],lol[])
{
   for(new i = 0; i != strlen(string);++i)
   {
       if(!strcmp(lol,"uppercase"))
       {
           if(string[i] >= 'A' && string[i] <= 'Z') return printf("It has uppercase letters.");
       }
       else if(!strcmp(lol,"lowercase"))
       {
           if(string[i] >= 'a' && string[i] <= 'z') return printf("It has lowercase letters.");
       }
       else if(!strcmp(lol,"numbers"))
       {
           if(string[i] >= '0' && string[i] <= '9') return printf("It has numbers");
       }
   }
   return 0;
}

//usage...
new blahblah[] = "abcdefkddkds2";
check(blahblah,"numbers");
new blahblah[] = "123232313FFSFFDSa";
check(blahblah,"lowercase");
new blahblah[] = "asdidsjaij123j21ij321A";
check(blahblah,"uppercase");
This way is pretty simple... instead of what he did..
Reply
#5

I'll add some more features to this function and publish somewhere so others can use it too

Let's go piece by piece:
pawn Code:
new valid = true;
new patterns[3][] = {
    { "ABCDEFGHIJKLMNOPRSTUWXYZ" },
    { "abcdefghijklmnoprstuwxyz" },
    { "0123456789" }
};
Before we start, we assume the string is valid. Next we define patterns - the string has to contain at least one character of each pattern. You can define your own, like "A4G.?" - the string can only be vaild if it contains A, 4, G, . or ? (assuming there are no other patterns).

pawn Code:
for(new i = 0; i < sizeof(patterns); i++) {
    for(new j = 0; j < strlen(patterns[i]); j++) { /* (...) */ }
}
finish_now:
Now we loop through each pattern, and each character in each pattern
pawn Code:
new cchar = patterns[i][j]; //We need char
new sub[2];
format(sub, sizeof(sub), "%s", cchar);
This a little hacky, we need simple example
pawn Code:
new string[6] = "Hello";
printf("%s", string[2]);
I expected this to print single character at position 3, but nope. It will print piece of string from the character at 3 position
Quote:

llo

If you want single char you need
pawn Code:
printf("%c", string[2]);
//to store in variable:
new singlechar = string[2];
I needed string so I used format (now I think strmid would be better solution). Anyway
pawn Code:
if(-1 != strfind(string, sub)) break;
If character from current pattern is present in string - go and check next pattern
pawn Code:
else {
    if(j == (strlen(patterns[i])-1)) {
        valid = false;
        goto finish_now;
    }
}
The character in current set was not found, and we are on last iteration. The string is now invalid. Check valid as false, and jump out of both loops, because we don't care about other patterns now.

Return valid variable

@Up: Hey, I had idea how to do this that way, don't blame me :c Also you can change your solution to not check strcmp on each loop iteration
Reply
#6

@leonardo1434 pretty nasty, what if I use for example Serbian latin? It will not work properly.
With Misiur's example it is editable much more than your one.
Reply
#7

microd, i didn't made it for serbian latin or whatever... i just made it for what he requested which is about english, since here it's english language it should be in english.

@yeh.. strcmp is slow, i could do the same with the string lol and check if it's equal.
@@ i don't use strfind, idk, i don't like it.
Reply
#8

You're somewhat vague in your first post. If you only need to check if the strings contains any combination of uppercase characters, lowercase characters and numbers, but not any other character, then this should suffice:
pawn Code:
noSpecialChars(string[])
{
    for(new i = strlen(string); i > 0; i--)
        if(!('A' <= string[i] <= 'Z') && !('a' <= string[i] <= 'z') && !('0' <= string[i] <= '9')) return false;

    return true;
}
Reply
#9

Quote:
Originally Posted by Vince
View Post
You're somewhat vague in your first post. If you only need to check if the strings contains any combination of uppercase characters, lowercase characters and numbers, but not any other character, then this should suffice:
pawn Code:
noSpecialChars(string[])
{
    for(new i = strlen(string); i > 0; i--)
        if(!('A' <= string[i] <= 'Z') && !('a' <= string[i] <= 'z') && !('0' <= string[i] <= '9')) return false;

    return true;
}
I mean wow now thats something awesome ,exactly what i wanted. Thanks hella to you Mr.Vince! (Rep+6 Added)

Can you give me an explanatory output of the following code? And i want to know how exactly does it process (working) ?
Reply
#10

Lol.... This is the inverse of my function. See the explanation.

pawn Code:
for(new i = strlen(string); i > 0; i--) // making a loop, telling the loop to start from the end of the string, and go down until 0.
if(!('A' <= string[i] <= 'Z') // in case is "A" bigger or equal to the position it was found(the pos that is located the character) or "Z" is less or equal to the position it was found.(the pos that is located the character) - basically,inverting the signals.
&& !('a' <= string[i] <= 'z')  // same thing as above...
&& !('0' <= string[i] <= '9'))// same thing as above, but with numbers...
return false; // return false... in case of any of these  statements are true.
return true; // returning true, if all of these statements are false.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)