当前位置:网站首页>设计一个函数print打印字符串,如果只传string型参数s,则字符串长度跟10比较,大于10,打印前10个字符,小于10,全部输出s;如果传string型参数s和int型n,则字符串长度跟n比
设计一个函数print打印字符串,如果只传string型参数s,则字符串长度跟10比较,大于10,打印前10个字符,小于10,全部输出s;如果传string型参数s和int型n,则字符串长度跟n比
2022-07-24 05:16:00 【陌小呆^O^】
#include <iostream>
#include <string>
using namespace std;
void print(string s)
{
cout << "第一种:" << endl;
if (s.length() > 10)
cout << s.substr(0, 10) << endl;
else
cout << s << endl;
}
void print(string s, int n)
{
cout << "第二种:" << endl;
if (s.length() > n)
cout << s.substr(0, n) << endl;
else
cout << s << endl;
}
void main()
{
string s;
cout << "请输入字符:";
cin >> s;
int n;
cout << "n=";
cin >> n;
print(s);
print(s, n);
}边栏推荐
猜你喜欢
随机推荐
Relational database 10 minutes to understand MySQL
VS 调试
Tips for using the built-in variable props of BeanShell
力扣、牛客网->链表相关题目(篇一)(c语言)
On the dilemma faced by non transferable reputation points NFT SBTS
MySQL insight
MQTT学习
View progress!!! RPM installation!!!
Skills of BeanShell dealing with JSON
Optional consistency
Add, delete, modify and check JDBC
Cmake笔记
Un7.23: how to install MySQL on linix?
reflex
Theoretical basis of machine learning
安装Pytorch+anaconda+cuda+cudnn
谈谈对未来的想法
好的程序员与不好的程序员
关于numpy基础用法的一个整理
Web development









