Simplest, shortest way to count capital letters in a string with php?

Asked
Active3 hr before
Viewed126 times

8 Answers

count
90%

The method to count capital letters in turbocommons has evolved to a method that can count upper case and lower case characters on any string. You can check it here:,I am looking for the shortest, simplest and most elegant way to count the number of capital letters in a given string.,It's not the shortest, but it is arguably the simplest as a regex doesn't have to be executed. Normally I'd say this should be faster as the logic and checks are simple, but PHP always surprises me with how fast and slow some things are when compared to others., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

function count_capitals($s) {
   return mb_strlen(preg_replace('![^A-Z]+!', '', $s));
}
load more v
88%

Scan string str from 0 to length-1.,if(str[i] >= 65 and str[i] <=90), then it is uppercase letter,  ,if(str[i] >= 48 and str[i] <=57), then it is number,  ,if(str[i] >= 97 and str[i] <=122), then it is lowercase letter,  

Given a string, write a program to count the occurrence of Lowercase characters, Uppercase characters, Special characters, and Numeric values. 
Examples: 
 

Input: #GeeKs01fOr @gEEks07
Output:
   Upper
case letters: 5
Lower
case letters: 8
Numbers: 4
Special Characters: 2

Input: * GeEkS4GeEkS *
   Output:
   Upper
case letters: 6
Lower
case letters: 4
Numbers: 1
Special Characters: 2

Output: 
 

Upper
case letters: 5
Lower
case letters: 8
Number: 4
Special characters: 2
load more v
72%

I am looking for the shortest, easiest and most elegant way to count the number of capital letters in a given string.,https://turbocommons.org/en/app/stringutils/count-capital-letters,https://turbocommons.org/en/blog/2019-10-15/count-capital-letters-in-string-javascript-typescript-php,The method of counting capital letters in turbocommons has become a method that can read capital and lowercase letters on any line. You can check it out here:

function count_capitals($s) {
   return strlen(preg_replace('![^AZ]+!', '', $s));
}
load more v
65%

ucfirst — Make a string's first character uppercase,(PHP 4, PHP 5, PHP 7, PHP 8),lcfirst() - Make a string's first character lowercase,ucwords() - Uppercase the first character of each word in a string

load more v
75%

Have another way to solve this solution? Contribute your code (and comments) through Disqus.,The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.,a) transform a string all uppercase letters. b) transform a string all lowercase letters. c) make a string's first character uppercase. d) make a string's first character of all the words uppercase.,Scala Programming Exercises, Practice, Solution

PHP Code:

< ? php
//all uppercase letters
print(strtoupper("the quick brown fox jumps over the lazy dog.")).
"\n";
//all lowercase letters
print(strtolower("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")).
"\n";
// make a string's first character uppercase
print(ucfirst("the quick brown fox jumps over the lazy dog.")).
"\n";
// make a string's first character of all the words uppercase
print(ucwords("the quick brown fox jumps over the lazy dog.")).
"\n"; ?
>

Sample Output:

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
the quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog.
The Quick Brown Fox Jumps Over The Lazy Dog.
load more v
40%

ucfirst() - It converts the first character of a string into uppercase.,lcfirst() - It converts the first character of a string into lowercase.,$string (required): This is a mandatory parameter of this function which specifies a string to convert into uppercase. In simple words - it is a string which is converted into uppercase.,ucwords() - It converts the first character of each word in a string into uppercase.

WELCOME TO JAVATPOINT
load more v
22%

string = raw_input("Enter string:")
count1 = 0
count2 = 0
for i in string:
   if (i.islower()):
      count1 = count1 + 1
elif(i.isupper()):
   count2 = count2 + 1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)
load more v
60%

The below Code will count Uppercase letters in JavaScript,How to count capital letters in JavaScript ( Uppercase letters count ),How to count small letters in JavaScript ( Lowercase letters count ),The below Code will count small letters in JavaScript

The below Code will count Uppercase letters in JavaScript

<!DOCTYPE html>
<html>
<head>
  <title>Count capital letters in JavaScript</title>
</head>
<body>
    Enter some text: <br>
    <input type="text" id="textbox">
    <button onclick="count()">
  <br>
  
  <script type="text/javascript">
    function count(){
      var mytext= document.getElementById("textbox").value;
      var numUpper = (mytext.match(/[A-Z]/g) || []).length;
      alert(numUpper);
    }
  </script>

</body>
</html>
load more v

Other "count-undefined" queries related to "Simplest, shortest way to count capital letters in a string with php?"