简体   繁体   中英

Is there a way to check for a certain amount of uppercase characters in a string?

How can I check for the amount of AZ characters in a string? Is there a specific function I can use to do this or maybe a regular expression that could return the count?

Assuming the message is a string this should work well:

message = "This is a message."
up_character_list = []
for character in message:
    if (character.isalpha()):
        if character.isupper():
            up_character_list.append(character)

if (len(up_character_list) > 6):
    # The message has more than 6 upper case alphabets.
    # Do what you want here.

It iterates through the string, first checks if the character in the string is a alphabet, then check if it's upper case, if both are true it adds the character to a list. You can then find the length of the list and do anything you want from there.

You can take a look at Regular Expressions (sometimes called Regex). It allows to search for a certain pattern in your string.

I didnt understand but as i know in programming u can compare 2 strings to check if they are the same:

string a,b ;
a= "hello";
b= "hello";
if(a==b)
return 1 ;

The string is an array of characters which it means each string you type have a number

string a = "hello"

this string have 5 character which it means if you tell your compiler to print the letter in the fifth place, you can just like arrays

//pay attention im coding in c++ it must work in java script
cout << a[4]//it means to print the letter in place 5

there is a command that tells you the size of the array you have

a.size();//this is the command that tells you the size of your array which is string in our case
cout << a.size() ;// the answer is 5 

in case you had a string with spaces then you should use if statments why? because you will have the size of the array but what if the array had spaces it will make its size bigger thats why you must type a little code like this

#include <iostream>
#include <string>
using namespace std ;
int main ()
{
string The_string = "hello world" ;
  char something_to_store_letters_on;//since i will store space i will use char
     int counter{a.size()};//here i tell the counter that its 11 the size of the the string array
for(i=0;i<a.size(); i++){
 The_string = something_to_store_letters_on.at(i);//in this for loop im storing in char letter by letter to check if its space or not

    if(something_to_store_letters_on==' ')//here i say if its space then the counter have 11(size of string array) to decrease it by 1
    counter-- ;
}
cout << counter ;// here i have the number of the charactes with out spaces
}

This is the answer of the Q how to check how letters in the string with or with out spaces I tried to make it simple as possible if you know arrays then you will have no problem understanding that. I hope i helped you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM