当前位置:编程学习 > VC++ >>

VC++指针编程

编程输入一行文字,找出其中的大写字母、空格、数字以及其它字符各有多少,要求应用指针来编程(用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;

}

上一个:vc++编辑问题
下一个:VC++遇到的问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,