练习:
提示并输入一个字符串,统计该字符串中字母、数字、空格、其他字符的个数并输出
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
string str;
cout <<"请输入字符串: ";
getline(cin,str);
int len;
len = str.size();
for(int i = 0;i < len;i++)
{
if((str.at(i)>=65&&str.at(i)<=90)||(str.at(i)>=97&&str.at(i)<=122))
{
num1++;
}
else if(str.at(i)>=48&&str.at(i)<=57)
{
num2++;
}
else if(str.at(i)==' ')
{
num3++;
}
else
{
num4++;
}
}
cout<<"字母有:"<<num1<<endl;
cout<<"数字有:"<<num2<<endl;
cout<<"空格有:"<<num3<<endl;
cout<<"其他字符有:"<<num4<<endl;
return 0;
}
使用cout实现输出斐波那契前20项的值
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 1;
int c;
cout << a << " " << b << " ";
for(int i = 3;i<=20;i++)
{
c = a + b;
cout << c << " ";
a = b;
b = c;
}
return 0;
}
使用cin和cout完成,提示并输入一个字符,判断该字符是大写还是小写,如果是大写字母,则转变成对应的小写字母输出,如果是小写字母,则转变成对应的大写字母输出,如果是其他字符,则转变成 '*' 并输出
#include <iostream>
using namespace std;
int main()
{
char buf;
cout << "请输入一个字符: ";
cin >> buf;
if(buf>=65 && buf<=90)
{
buf+=32;
cout << buf;
}
else if(buf>=97&&buf<=122)
{
buf-=32;
cout << buf;
}
else
{
cout << " * ";
}
return 0;
}