找出给定字符串中大写字符(即'A'-'Z')的个数。
数据范围:字符串长度:
字符串中可能包含空格或其他字符
进阶:时间复杂度:,空间复杂度:
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ char[] ch = sc.nextLine().toCharArray(); int nums = 0; for(int i=0;i<ch.length;i++){ if(ch[i]>='A' && ch[i]<='Z'){ nums++; } } System.out.println(nums); } } }
while True: try: a = input() count = 0 for c in a: if c.isupper(): count += 1 print(count) except: break
s = input() l = [] for i in s: if i.isupper(): l.append(i) print(len(l))
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String s = in.nextLine(); int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') { count++; } } System.out.println(count); } } }
package HUAWEI3; import java.util.Scanner; public class Demo04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = sc.next(); int num = 0; for(int i=0;i<s.length();i++){ String temp = String.valueOf(s.charAt(i)); if(temp.matches("[A-Z]")){ num++; } } System.out.println(num); } sc.close(); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while (sc.hasNext()) { String s = sc.next(); int cnt = 0; for(int i=0; i<s.length(); i++) { if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') { cnt ++; } } System.out.println(cnt); } } }
import sys for line in sys.stdin: uplis = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" nn = [i for i in line if(i in uplis)] print(len(nn))
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while(scanner.hasNextLine()){ String str = scanner.nextLine(); System.out.println(fun(str)); } } public static int fun(String arr){ return arr.replaceAll("[^A-Z]*","").length(); } }
#include <iostream> (720)#include <string> using namespace std; int main() { string a; while(cin>>a) { int b=0; for(int i=0;i<a.size();i++) { if((a[i]>64)&&(a[i]<97)) { b++; } } cout<<b<<endl; } return 0; }
#include <iostream> (720)#include <string> #include <algorithm> int main(int argc, char* argv[]) { std::string str; while (std::cin >> str) std::cout << std::count_if(str.cbegin(), str.cend(), [](const char c)->bool { return isupper(c); }) << std::endl; return 0; }
#include <string> #include <iostream> using namespace std; int CalcCapital(string &str) { int len = str.length(); int count = 0; for(int i=0; i<len; ++i) { if(str[i]<='Z'&&str[i]>='A') count++; } return count; } int main() { string str; while(getline(cin, str)) { cout << CalcCapital(str) << endl; } return 0; }
#include<iostream> #include<cstring> using namespace std; int main(){ char ch[1000]; while(cin>>ch){ int count=0; for(int i=0;i<strlen(ch);i++) if(ch[i]>='A'&&ch[i]<='Z') count++; cout<<count<<endl; } }没什么好说的啦,这个就非常基础
# python3 一行解法 while 1: try: print(len(list(filter(lambda x: x.isupper(),input())))) except: break
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String str=sc.nextLine(); char[] ch=str.toCharArray(); int count=0; for (int i = 0; i < ch.length; i++) { if (ch[i]>='A' && ch[i]<='Z') { count++; } } System.out.println(count); } sc.close(); } }
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner in = new Scanner(System.in); while (in.hasNext()){ String str = in.next(); int n=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)>='A' && str.charAt(i)<='Z') n++; } System.out.println(n); } in.close(); } }