编写函数,统计一个字符串中大写字母、小写字母和数字各有多少.

编写函数,统计一个字符串中大写字母、小写字母和数字各有多少,其中字符串的输入和大小写字母及数字的统计结果在主函数中输出。
用指针和函数做..

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来确认执行成功。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-21
class Program
{
public static string mymethod(string s)
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
for (int i = 0; i < s.Length; i++)
{
char C = s[i];
int ASC = C;
if (ASC >= 65 && ASC <= 90)
{
num1 += 1;
}
else if (ASC >= 97 && ASC <= 122)
{
num2 += 1;
}
else if (ASC >= 48 && ASC <= 57)
{
num3 += 1;
}
}
string result = "字符串中大写字母数为" + num1 + "个,小写字母数为" + num2 + "个,数字为" + num3 + "个";
return result;
}
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串");
string s= Console.ReadLine();
Console.WriteLine(mymethod(s));
Console.ReadKey();
}
}
第2个回答  2011-12-21
#include <stdio.h>
#include <string.h>
#define n 50
void main()
{ int i=0,littlechar=0,bigchar=0,space=0,num=0,other=0;
char a[n];
gets(a);
while(a[i]!='\0')
{if(a[i]>='a'&&a[i]<='z') littlechar++;

else if(a[i]>='A'&&a[i]<='Z') bigchar++;
else if(a[i]>='0'&&a[i]<='9') num++;
else if(a[i]=='') space++;
else other++;
i++;
}

printf("大写字母%d\n小写字母%d\n空格%d\n数字%d\n其他%d\n",bigchar,littlechar,space,num,other);

}
第3个回答  2011-12-21
#include <stdio.h>
#define N 200
void count(char *);
int main()
{
char *ch,chr;
ch=malloc(N+1);
printf("请输入输入一行字符:\n");
gets(ch);
count(ch);
getchar();
}

void count(char *ch)
{
char *temp=ch;
int i, upper=0,lower=0,digit=0;
while(*ch != '\0')
{
if(*ch>='A' && *ch<='Z' )
upper++;
else if(*ch>='a' && *ch<='z')
lower++;
else if(*ch>='0' && *ch<='9')
digit++;
ch++;
}
printf("大写字母有%d个,小写字母有%d个,数字有%d个\n",upper,lower,digit);
}本回答被提问者采纳
相似回答