大写字母出现的次数并且打印

import java.util.Scanner;

/** * 2.编写一个java程序,提示用户输入一个字符串,要求字符串中必须存在字母(需要代码判断) a. 若不符合要求,则提示用户重新输入直到符合要求为止 b. 若符合要求 ,则判断字符串中的大写字母出现的次数并且打印 */public class Test02 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while (true){            System.out.println("输入一个字符串,要求字符串中必须存在字母");            String string = scanner.next();            boolean method1 = method1(string);            //如果包含字母            if(method1){                int sum = method2(string);                System.out.println(sum);                break;            }        }

    }

    /**     * 需要统计大写字母的字符串     * @param s     * @return     */    public static int method2(String s){        int sum = 0;        char[] chars = s.toCharArray();        for (int i = 0; i < chars.length; i++) {            if(chars[i] <= ‘Z‘ && chars[i] >= ‘A‘){                System.out.println(chars[i]);                //出现大写字母加1                sum++;            }        }        return sum;

    }    /**     * 首先判断输入的是否包含字母     * 该方法适用于检测字符串中是否包含字母(z-a,Z-A)     * @param s  需要检测的字符串     * @return   是否有字母 true 是有字母  false 没有字母     */    public static boolean method1(String s){        //先把字符串变为字符数组        char[] chars = s.toCharArray();        //遍历数组        for (int i = 0; i < chars.length; i++) {            //包含字母的情况            if((chars[i] <= ‘Z‘ && chars[i] >= ‘A‘) || (chars[i] >= ‘a‘ && chars[i] <= ‘z‘)){                return true;            }        }        return false;    }}

原文地址:https://www.cnblogs.com/YRSWBY2016/p/12000053.html

时间: 2024-08-03 21:29:39

大写字母出现的次数并且打印的相关文章

统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。

/** * A:案例演示 * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数. * [email protected]#$%^ * 分析:字符串是有字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符 * 如果包含就让计数器变量自增 */ public static void main(String[] args) { String s = "[email protected]#$%^"; int big = 0; int smal

利用printf()函数,打印一个由*号组成的大写字母A

#include <stdio.h > void main() { printf(" *\n") ; printf(" * *\n"); printf(" *****\n"); printf(" * *\n"); printf(" * *\n") ; } 利用printf()函数,打印一个由*号组成的大写字母A,布布扣,bubuko.com

打印大写字母三角形

题目内容: 输入行数和起始字母,输出大写字母等腰三角形.输出的字母是循环的,即递增到字母 'Z' 以后输出 'A' :递减到字母 'A' 以后输出 'Z'.例如: 输入样例: 4[空格]X[回车] 输出样例: [空格][空格][空格]X[回车] [空格][空格]YZY[回车] [空格]ZABAZ[回车] ABCDCBA[回车] 1 #include <stdio.h> 2 int main() 3 { 4 int i,j,k; 5 int m; 6 char n; 7 scanf("

java编程:输入一串小写字符串,统计每个字母出现的次数

*需求:统计字符串中每个字母: * 说明:编写程序,提示用户输入一个字符串, * 然后统计字符串中每个字母出现的个数,忽略字母的大小写. * * 原理: * 1.使用String类中的toLowerCase()方法,将字符串中的大写字母转换成小写形式. * 2.构造一个具有26个int值得数组ch ,每个元素记录一个字母出现的次数. *     即,ch[0]记录a的个数,ch[1]记录b的个数. * 3.对字符中的每一个字符,判断其是否小写字母,如果是,则数组中的相应计数器加1. 第一种风格的

java集合TreeMap应用---求一个字符串中,每一个字母出现的次数

package cn.itcast.p1.map.test; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; public class TestMap { /** * 练习: * "fdgavcbsacdfs+++AA&&BBB" 获取该字符串中,每一个字母出现的次数. * 要求打印结果是:a(2)b(1)...; * 思路: * 对于结果的分析发现,字母和次数之间存在

c语言求字符串中大写字母个数,单词个数,子串个数及一个整数是否为回文数

#include <stdio.h> #include <ctype.h> #pragma mark 统计从终端输入的字符中每个大写字母的个数.用#号作为输入结束标志 int main() { int num[26] = {0}, i; char c; while ((c = getchar())!='#') { if (isupper(c)) { num[c-65]++; } } for (int i = 0; i<26; i++) { if (num[i]) { prin

待解:通过把第6位设置为0使小写字母都变成大写字母

根据Unicode/ASCII字符集的定义,小写字母与大写字母的区别只是前者比后者整整大32.因此…… 1 class UpCase { 2 public static void main(String[] args) { 3 char ch; 4 5 for (int i = 0; i < 10; i++) { 6 ch = (char) ('a' + i); 7 System.out.print(ch); 8 9 // This statement turns off the 6th bit

把一个string串的所有小写字母转成大写字母的例子来看看看全局函数的使用

今天写了一个小例子,把字符串里面的所有小写字母全部转换成大写字母http://blog.csdn.net/yasaken/article/details/7303903 1 #include "stdafx.h" 2 #include <string> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 int _tmain(int argc, _TCHA

汇编语言——统计一个字符串中的大写字母、小写字母、数字和其他字符的个数,并显示

;统计字符串中大写字母.小写字母.数字.其他字符的个数DATAS SEGMENT buf db '12ADdf#gh592HKL*','$' tp1 db 0;大写字母个数 tp2 db 0;小写字母个数 tp3 db 0;数字的个数 tp4 db 0;其他字符的个数 str1 db 'the number of big is:','$' str2 db 'the number of small is:','$' str3 db 'the number of number is:','$' st