Page 1 of 1

[Chat] upper case detection

Posted: Mon Feb 14, 2011 10:16 am
by gl1tch
We had a small discussion about detecting upper case in chat strings etc, here is an example of how you could do that with regex and php (i leave it to you to discover the correct functions in java)
Code: Select all
<?php

$chatstring='I\'ll tell you what I want, WHAT I REALLY REALLY WANT!';

$alllettersregex='/\w/';
$lowercaseregex='/[a-z]/';
$uppercaseregex='/[A-Z]/';

$lettercount=preg_match_all($alllettersregex,$chatstring,$letterarray);
$uppercount=preg_match_all($uppercaseregex,$chatstring,$upperarray);
$lowercount=preg_match_all($lowercaseregex,$chatstring,$lowerarray);

$percentupper=(($uppercount/$lettercount)*100);


echo "String is $percentupper percent upper case";

?>

Re: [Chat] upper case detection

Posted: Fri Feb 18, 2011 8:00 am
by Sharingan616
Code: Select all
public class UpperCasePercent{
  public static void main(String[] args){
    
    String playertext = "Why did the chicken cross the road? TELL ME WHY THE CHICKEN CROSSED THE ROAD!";
    int upperCaseCount = 0;
    char letter;
    
    for (int i = 0; i < playertext.length(); i++)
    {
      letter = playertext.charAt(i);
      if(Character.isUpperCase(letter))
        upperCaseCount ++;
    }
    
    double percentage = ((upperCaseCount*100)/playertext.length());
    System.out.println(percentage+"%");
    
  }
}
This code returns 44.0%.

:D

Re: [Chat] upper case detection

Posted: Fri Feb 18, 2011 11:53 am
by gl1tch
Sharingan616 wrote: This code returns 44.0%.

:D
There are 34 caps and 27 lowers

you might want to revise your code.

Re: [Chat] upper case detection

Posted: Fri Feb 18, 2011 12:38 pm
by Sharingan616
The code returns 44% because technically the string is 44% uppercase and 56% lowercase, just normal spaces, and other punctuation.
Code: Select all
public class UpperCasePercent{
  public static void main(String[] args){
    
    String playertext = "Why did the chicken cross the road? TELL ME WHY THE CHICKEN CROSSED THE ROAD!";
    int upperCaseCount = 0;
    int lowerCaseCount = 0;
    char letter;
    
    for (int i = 0; i < playertext.length(); i++)
    {
      letter = playertext.charAt(i);
      if(Character.isUpperCase(letter))
        upperCaseCount ++;
      if(Character.isLowerCase(letter))
        lowerCaseCount ++;
    }
    
    int upperpercentage = ((upperCaseCount*100)/playertext.length());
    int lowerpercentage = ((lowerCaseCount*100)/playertext.length());
    System.out.print("The string is "+upperpercentage+"% uppercase and ");
    System.out.println(lowerpercentage+"% lowercase.");
  }
}
This code prints this:
"The string is 44% uppercase and 35% lowercase."

Then I guess you could write if the uppercase percentage is greater then the lowercase percentage, kick the person typing or kick them if they do it 5 in a row or something.