VC++指针编程
编程输入一行文字,找出其中的大写字母、空格、数字以及其它字符各有多少,要求应用指针来编程(用c++编程不要用C)
编程输入一行文字,找出其中的大写字母、空格、数字以及其它字符各有多少,要求应用指针来编程(用c++编程不要用C)
答案:#include <iostream>
#include <string>
void func(char* A,int &a,int &b,int &c);
using namespace std;
int main()
{
char *ch=new char[]();
int x,y,z;
x=y=z=0;
cout<<"请输入一行字符串:"<<endl;
cin>>ch;
for(int i=0;i<strlen(ch);i++)
{
func(&ch[i],x,y,z);
}
cout<<"这行字符串中:"<<endl;
cout<<"数字有"<<x<<"个,大写字母有"<<y<<"个,小写字母有"<<z<<"个!"<<endl;
return 0;
}
void func(char* A,int &a,int &b,int &c)
{
if((*A)>='0'&&(*A)<='9') a++;
else if((*A)>='A'&&(*A)<='Z') b++;
else if((*A)>='a'&&(*A)<='z') c++;
}到 http://www.zhubajie.com/task/?registe=2556414
找吧 绝对有你想要的假设输入的字符串为str - char*
一些函数,不记得了,自己去库里找下,这里我随便用2函数表示IsDig,IsUpChar,分别是判读是否为数字和大写字符的
int tmp1=0,tmp2=0,tmp 3=0; //分别表示大写字母,空格,数字
while(*str)
{
if(*str == " ") tmp2++ ; //空格
else if( IsDig(*str)) tmp3++; //数字
else if(IsUpChar(*str)) tmp1++;//大写字符
str++;
}
然后输出了:cout<<tmp1<<tmp2<<tmp3;
-----------------------------------------------
顺便自己写两个函数给你把,分别用于判断大写字符和数字的
bool IsNum(char c){
char *num = "123456789"while(*num)
{
if(*num == c) return true;
num++;
}
return false;
}
/--
判断大写字母的
bool IsUpChar(char c)
{
for(int i = 'A' ; i<= 'Z' ;i++){
if(c == (char)i) return true;
}
return false;
}