第一篇:C#中鍵盤輸入一串字符,輸出該字符包含字母、數(shù)字個數(shù)
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
namespace study
{
class Program
{
static void Main(string[] args){
{
{
numberSum=}
{
smallSum=}
{
bigSum=}
{
elseSum=}
}
}
}
}
Console.WriteLine(“請輸入字符:”);string a = Console.ReadLine();int numberSum=0,smallSum=0,bigSum=0,elseSum=0;for(int i = 0;i < a.Length;i++)if(a[i] >= 48 && a[i] <= 57)numberSum+1;else if(a[i] >= 97 && a[i] <= 122)smallSum+1;else if(a[i] >= 65 && a[i] <= 90)bigSum+1;else elseSum+1;Console.WriteLine(“數(shù)字有{0}個”, numberSum);Console.WriteLine(“小寫字母有{0}個”, smallSum);Console.WriteLine(“大寫字母有{0}個”, bigSum);Console.WriteLine(“其他有{0}個”, elseSum);
第二篇:輸入一行字符,分別統(tǒng)計出其中英文、空格、數(shù)字和其他字符的個數(shù)
#include
int main()
{char ch;int A=0,B=0,C=0,D=0;
printf(“A(字符)B(空格)C(數(shù)字)D(其它):n”);
printf(“請輸入一串字符:”);
while((ch=getchar())!='n')
{if('a'<=ch && ch<='z' || 'A'<=ch && ch<='Z')A=A+1;
else if('0'<=ch&&ch<='9')C=C+1;
else if(ch==' ')B=B+1;
else D=D+1;
}
printf(“英文字母、空格、數(shù)字、其他字符的個數(shù)分別為:%d,%d,%d,%dn”,A,B,C,D);return 0;
}
第三篇:將字符串tt中的小寫字母都改為對應(yīng)的大寫字母,其他字符不變。例如,若輸入Ab,cD,則輸出AB,CD
、下列給定程序中,函數(shù)fun的功能是:將字符串tt中的小寫字母都改為對應(yīng)的大寫字母,其他字符不變。例如,若輸入“Ab,cD”,則輸出“AB,CD”。
請改正程序中的錯誤,使它能統(tǒng)計出正確的結(jié)果。
注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
試題程序:
#include
#include
#include
char*fun(char tt[])
{
int i;
/********found********/
for(i=0;tt[];i++)
if((tt[i]>='a')||(tt[i]<='z'))
/********found********/
tt[i]+=32;
return(tt);
}
main()
{
char tt[81];
printf(“Please enter a string: ”);
gets(tt);
printf(“nThe result string is: %ns”,fun(tt));
}
(1)錯誤:||
(2)錯誤:tt[i] += 32 正確:應(yīng)改為 tt[i]-=32 正確:&&