C++编程基础题

题目:

1.字符串转为int

输入1234

输出1234

注意:输入的必须为字符串,输出必须为int型
2.偶数位转为大写
例如:
输入:lanzhihui is a boy!
输出:lAnZhIhUi Is A BoY!
注意:空格也算一位,符号不变.
3.每个单词偶数位转为大写
例如:
输入:lanzhihui is a boy!
输出:lAnZhIhUi iS a bOy!
注意:符号不变.
4.字符串转换为double
例如:
输入:123.456            //char a[10]

输出:123.456            //double n;

5.十进制转换为二进制
输入:10
输出:1010
6.将字符串中大写转换为小写,小写转换为大写
输入:LanzhiHUI
输出:lANZHIhui

7.输入一个十进制数转换为二到九进制的任意进制


答案:
1.字符串转为int
输入1234
输出1234
注意:输入的必须为字符串,输出必须为int型
#include<iostream>
#include<string>
using namespace std;


int main()
{
char ch[10];
char a[10];
int sum=0;
gets(ch);
for(int i=0;ch[i]!='\0';i++)
sum=ch[i]-'0'+sum*10;
a[0]=ch[0];
cout<<"输入"<<ch<<endl;
cout<<a<<endl;
cout<<"输出"<<sum<<endl;
system("pause");
//return 0;
}

2.偶数位转为大写
例如:
输入:lanzhihui is a boy!
输出:lAnZhIhUi Is A BoY!
注意:空格也算一位,符号不变.
#include<iostream>
#include<string>
using namespace std;

Int  main()
{
char ch[40]={0},temp[40]={0};
gets(ch);
int i=0,j=0;
while(ch[i]!='\0')
{
if(i%2==0&&ch[i]!=' ') 
{
temp[j++]=ch[i]-32;
i++;
}
else 
{
temp[j++]=ch[i];
i++;
}
}
cout<<ch<<endl;
cout<<temp<<endl;
system("pause");

return 0;
}

3.每个单词偶数位转为大写
例如:
输入:lanzhihui is a boy!
输出:lAnZhIhUi iS a bOy!
注意:符号不变.
#include<iostream>
#include<string>
using namespace std;

int main()
{
char ch[40]={0},temp[40]={0};
gets(ch);
int i=0,j=0;
while(ch[i]!='\0')
{
if(ch[i]==' ') 
j++;
if(j%2==0&&ch[i]!=' ')
{
temp[i]=ch[i]-32;
}
else 
{
temp[i]=ch[i];
}
i++;
j++;
}
cout<<ch<<endl;
cout<<temp<<endl;
system("pause");
}

4.字符串转换为double
例如:
输入:123.456            //char a[10]
输出:123.456            //double n;
#include<iostream>
#include<string>
using namespace std;

int main()
{
char ch[20];
//double t=123.4565;
//cout<<t<<endl;
double sum=0,num_10=1.0;;int i=0,j=0;
gets(ch);
for(i=0;ch[i]!='.';i++)
sum=ch[i]-'0'+sum*10;
for(j=1;ch[i+j]!='\0';j++)
{
num_10=10.0*num_10;
sum=(ch[i+j]-'0')/num_10+sum;
}
cout<<"输入"<<ch<<endl;
cout<<"输出"<<sum<<endl;
system("pause");
return 0;
}

/*5.十进制转换为二进制
输入:10
输出:1010
#include<iostream>
#include<string>
using namespace std;


int main()
{
int num,i=0,temp[10];
cin>>num;
while(num!=0)
{
temp[i]=num%2;
num=num/2;
i++;
}
//cout<<"i"<<endl;
for(int k=i-1;k>=0;k--)
cout<<temp[k];
system("pause");
return 0;
}
6.将字符串中大写转换为小写,小写转换为大写
输入:LanzhiHUI
输出:lANZHIhui

#include<iostream>
#include<string>
using namespace std;

int main()
{
char a[50];
gets(a);
for(int i=0;a[i]!=0;i++)
{
if((a[i]<'a')&&(a[i]!=' '))
a[i]=a[i]+32;
else if((a[i]>='a')&&(a[i]!=' '))
a[i]=a[i]-32;
else if(a[i]==' ')
a[i]=a[i];
}
cout<<a<<endl;
system("pause");
}
7、输入一个十进制数转换为二到九进制的任意进制
#include<iostream>
using namespace std;

int main()
{
int num,numsize,temp[30],i=0;
cout<<"输入该数和进制数"<<endl;
cin>>num>>numsize;
cout<<num<<endl;
while(num!=0)
{
temp[i]=num%numsize;
num=num/numsize;
i++;
}
//cout<<"i"<<endl;
for(int k=i-1;k>=0;k--)
cout<<temp[k];
system("pause");
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值