当前位置:网站首页>设计一个函数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);
}边栏推荐
- T 1-5
- T 6-10
- Pointer learning diary (IV) use structure and pointer (linked list)
- Problems encountered in configuring Yum source
- FTP file transfer protocol
- 【sklearn】数据预处理
- c2-随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习+转载整理
- Embedded system transplantation [3] - uboot burning and use
- C语言进阶篇 一.数据的存储
- Detailed explanation of string constant pool and intern() of string
猜你喜欢

Performance test process

Ia notes 2

Tips for using the built-in variable vars in BeanShell

C语言进阶篇 六.文件的操作

Tips for using the built-in variable props of BeanShell

How can NFT, whose stars enter the market against the market, get out of the independent market?

C语言入门篇 二.函数

安装Pytorch+anaconda+cuda+cudnn

一步一步带你学C(其一)
![Embedded system transplantation [3] - uboot burning and use](/img/36/69daec5f1fe41bd3d0433d60816bba.png)
Embedded system transplantation [3] - uboot burning and use
随机推荐
How can NFT, whose stars enter the market against the market, get out of the independent market?
C语言进阶篇 二. 指针
Relational database 10 minutes to understand MySQL
T 6-10
Industry relevance of stock price trend
scikit-learn笔记
Handwritten ORM framework
JDBC encapsulates a parent class to reduce code duplication
Career planning route
DNS domain name resolution service
ros启动非本机节点
T 11-20
)To feed back to the application layer or into multiple format documents:
What are the core strengths of a knowledge base that supports customers quickly?
Jersey2.25.1集成freemarker
Introduction to threads
【Pytorch】conv2d torchvision.transforms
Reading excerpts from Liu run's "bottom logic"
Tips for using the built-in variable vars in BeanShell
1、基于增量式生成遮挡与对抗抑制的行人再识别