编写函数,统计一个字符串中大写字母、小写字母和数字各有多少,其中字符串的输入和大小写字母及数字的统计结果在主函数中输出。
用指针和函数做..
package cn.itcast_06;
public class StringDemo3 {
public static void main(String[] args) {
// 定义一个字符串
String s = "Hello12345685757World";
// 定义三个统计变量
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
// 遍历字符串,得到每一个字符。
xfor (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
// 判断该字符到底是属于那种类型的
if (ch >= 'a' && ch <= 'z') {
xxsmallCount++;
} else if (ch >= 'A' && ch <= 'Z') {
xxbigCount++;
} else if (ch >= '0' && ch <= '9') {
xxnumberCount++;
}
}
// 输出结果。
System.out.println("大写字母" + bigCount + "个");
System.out.println("小写字母" + smallCount + "个");
System.out.println("数字" + numberCount + "个");
}
}
定义函数:
int system(constchar*string);
函数说明:
system()会调用fork()产生子进程,由子进程来调用/bin/sh-cstring来执行参数string字符串所代表的命令,此命>令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD信号会被暂时搁置,SIGINT和SIGQUIT信号则会被忽略。
返回值=-1:出现错误=0:调用成功但是没有出现子进程>0:成功退出的子进程的id如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。嵌入式物联网智能硬件等系统学习企鹅意义气呜呜吧久零就易,若参数string为空指针(NULL),则返回非零值>。
如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno来确认执行成功。