题目一:
写一个函数,判断用户输入的QQ号码是否合法,程序输入为字符串,输出为数字(合法输出QQ号,不合法输出0)。
假设合法QQ号码的规则:
1)全数字;
2)以1~3之间的数字开头;
3)最短5位,最长10位。
题目二:
把一个字符串中的所有空格都去掉。
#include<iostream>
#include<cstdlib>
#include<cctype>
using namespace std;
/**********************************************
*
*越界问题!
* LONG_MAX 214 748 3647
* LONG_MIN -214 748 3648
*
* ULONG_MAX 429 496 7295
*
***********************************************/
unsigned int getValidateQQ(char* str)
{
unsigned int qqnum=0;
char *c=str;
while(isspace(*c))
c++;
if(strlen(c)>10)
return 0;
else if(strlen(c)==10 && (*c)-'0'>3)
return 0;
while(isdigit(*c))
{
qqnum*=10;
qqnum+=(*c)-'0';
c++;
}
if(*c !='\0')
return 0;
if( qqnum >=10000 && qqnum<=3999999999)
return qqnum;
else
return 0;
}
/**********************************************
*
*char* 与 char []的区别!
*
*去掉空格
*
***********************************************/
char* removeSpace(char *str)
{
while(isspace(*str))
str++;
char* rtstr=(char*)malloc(sizeof(str)); //free
char *p1=rtstr,*p2=str;
while(*p2 !='\0')
{
while(isspace(*p2))
p2++;
if(p1 !=p2)
*p1 = *p2;
p1++;
p2++;
}
return rtstr;
}
int main(int argc,char** argv)
{
//测试 getValidateQQ
cout<<" 10abc "<< getValidateQQ(" 10abc")<<endl;
cout<<"0000 "<< getValidateQQ("0000")<<endl;
cout<<"10000 "<<getValidateQQ("10000")<<endl;
cout<<"101010101 "<<getValidateQQ("101010101")<<endl;
cout<<"3999999999 "<<getValidateQQ("3999999999")<<endl;
cout<<"4999999999 "<<getValidateQQ("4999999999")<<endl;
//测试 removeSpace
cout<<removeSpace(" abcd")<<endl;
cout<<removeSpace("abcd ")<<endl;
cout<<removeSpace("ab cd")<<endl;
cout<<removeSpace(" a b c d ")<<endl;
system("pause");
return 0;
}