第一種情況:
描述:寫出一個程序,接受一個有字母和數字組成的字符串,和一個字符,然後輸出輸入字符串中含有該字符的個數。不區分大小寫。
知識點:字符串,函數,指針
題目來源:內部整理
練習階段:初級
運行時間限制:10Sec
內存限制:128MByte
輸入:輸入一個有字母和數字組成的字符串,和一個字符。
輸出:輸出輸入字符串中含有該字符的個數。
樣例輸入:
ABCDEF
A
樣例輸出: 1
源程序:
#include<iostream>
using namespace std;
#define MAXSIZE 100
int main()
{
char c[MAXSIZE];
char ch;
int getCharCount(char c[],char ch);
gets(c);
ch=getchar();
int count;
count=getCharCount(c,ch);
cout<<count<<endl;
return 0;
}
int getCharCount(char c[],char ch)
{
int i=0;
char *p;
char chx;
p=c;
while(*p!='\0')
{
if(ch>='A'&&ch<='Z')//不區分大寫小寫
chx=ch+32;
else
chx=ch-32;
if(*p==ch||*p==chx)
i++;
p++;
}
return i;
}
運行結果:
第二種情況:
描述:編寫一個函數,計算字符串中含有的不同字符的個數。字符在ACSII碼範圍內(0~127)。不在範圍內的不作統計。
知識點:字符串,函數,指針
題目來源:內部整理
練習階段:初級
運行時間限制:10Sec
內存限制:128MByte
輸入:輸入N個字符,字符在ACSII碼範圍內(0~127)。
輸出:輸出字符的個數。
樣例輸入:abc
樣例輸出:3
源程序:
#include<iostream>
using namespace std;
#define MAXSIZE 100
int main()
{
char c[MAXSIZE];
int getCharCount(char c[]);
gets(c);
int count;
count=getCharCount(c);
cout<<count<<endl;
return 0;
}
int getCharCount(char c[])
{
char b[MAXSIZE],k=0;
int i=0,j=0;
b[0]=c[0];//把新字符存入另一個數組中。
int flag;
for(i=1;c[i]!='\0';i++)
{
flag=1;//標識
for(k=0;k<j+1;k++)//j爲b數組的下標,j+1就是b數組的長度。
{
if(c[i]==b[k])flag=0;//flag爲標識,若當前字符已存在於b數組,則flag=0;
}
if(flag)//當前字符c[i]不存在與b數組,就把c[i]存入b數組中。
{
j=j+1;
b[j]=c[i];
}
}
return j+1;
}
運行結果:
第三種情況:
描述:輸入一行字符,分別統計出包含英文字母、空格、數字和其它字符的個數。
/**
* 統計出英文字母字符的個數。
*
* @param str 需要輸入的字符串
* @return 英文字母的個數
*/
public static int getEnglishCharCount(String str)
{
return 0;
}
/**
* 統計出空格字符的個數。
*
* @param str 需要輸入的字符串
* @return 空格的個數
*/
public static int getBlankCharCount(String str)
{
return 0;
}
/**
* 統計出數字字符的個數。
*
* @param str 需要輸入的字符串
* @return 英文字母的個數
*/
public static int getNumberCharCount(String str)
{
return 0;
}
/**
* 統計出其它字符的個數。
*
* @param str 需要輸入的字符串
* @return 英文字母的個數
*/
public static int getOtherCharCount(String str)
{
return 0;
}
知識點:字符串
題目來源:內部整理
練習階段:初級
運行時間限制: 10Sec
內存限制:128MByte
輸入:輸入一行字符串,可以有空格
輸出:統計其中英文字符,空格字符,數字字符,其他字符的個數
樣例輸入:1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;0-=\\][
樣例輸出:
26
3
10
12
源程序:
#include<iostream>
#include<cstring>
using namespace std;
#define MAXSIZE 100
int main()
{
int getEnglishCharCount(string str);
int getBlankCharCount(string str);
int getNumberCharCount(string str);
int getOtherCharCount(string str);
char str[MAXSIZE];
gets(str);
int Eng_count,Bla_count,Num_count,Oth_count;
Eng_count=getEnglishCharCount(str);
Bla_count=getBlankCharCount(str);
Num_count=getNumberCharCount(str);
Oth_count=getOtherCharCount(str);
cout<<Eng_count<<endl;
cout<<Bla_count<<endl;
cout<<Num_count<<endl;
cout<<Oth_count<<endl;
return 0;
}
int getEnglishCharCount(string str)
{
int i=0,engcount=0;
while(str[i]!='\0')
{
if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
engcount++;
i++;
}
return engcount;
}
int getBlankCharCount(string str)
{
int i=0,blacount=0;
while(str[i]!='\0')
{
if((str[i]==' '))
blacount++;
i++;
}
return blacount;
}
int getNumberCharCount(string str)
{
int i=0,numcount=0;
while(str[i]!='\0')
{
if(str[i]>='0'&&str[i]<='9')
numcount++;
i++;
}
return numcount;
}
int getOtherCharCount(string str)
{
int i=0;
int e,b,n;
int othcount;
e=getEnglishCharCount(str);
b=getBlankCharCount(str);
n=getNumberCharCount(str);
while(str[i]!='\0')
i++;
othcount=i-e-b-n;
return othcount;
}
運行結果: