各大计算机公司 笔试及面试 题目 - 腾讯(字符串)

题目一:

写一个函数,判断用户输入的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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值