I want to check that all characters in a word are capital or not

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alimsdb
    New Member
    • Feb 2008
    • 7

    I want to check that all characters in a word are capital or not

    This code I have written but even Good not contains all capital words like GOOD the output of my code is All word is capital which is wrong.

    Can any one help me to solve this problem.

    [CODE=PERL]my $element = "Good";
    if ($element =~ /[A-Z]/g)
    {
    print "All word is capital";
    }
    else
    {
    print "Not all word capital"
    }[/CODE]
    Last edited by eWish; Mar 6 '08, 07:42 AM. Reason: Please use code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    Do a complete string match. ^ stands for begining of string and $ for end of string. using /g won't server the purpose. This will tell interpreter to not stop with first search and continue further, but will not return false if pattern is not found after the first match.

    Code:
    if ($element =~ /^[A-Z]+$/)
    {
     print "Whole word is capital";
    }
    else
    {
    print "Whole word is not capital"
    }

    Comment

    Working...