3 回答

TA貢獻1995條經驗 獲得超2個贊
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來確認執行成功。

TA貢獻1847條經驗 獲得超11個贊
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();
}
}

TA貢獻1779條經驗 獲得超6個贊
#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 回答
- 0 關注
- 154 瀏覽
添加回答
舉報