当前位置:首页 > C++知识 > 正文内容

字符串的输入输出汇总

亿万年的星光3年前 (2022-07-23)C++知识6895
  1. 做字符串的题目的时候,经常会遇到输入输出不对的情况,这篇文章就简单总结一下字符串常见的输入输出。


2.cin

基本操作:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	cin>>a;  				//hello
	cout<<strlen(a)<<endl; 	                // 5
	cout<<a; 				//hello
	return 0;
}

能否读空格:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	cin>>a;  				//hello world
	cout<<strlen(a)<<endl; 	                // 5
	cout<<a; 				//hello
	return 0;
}

多个数组读入

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	char b[100];
	cin>>a>>b;  			//hello world!
	cout<<strlen(a)<<endl; 	        // 5
	cout<<strlen(b)<<endl; 	        // 6
	cout<<a<<endl; 				//hello
	cout<<b<<endl; 				//hello!
	return 0;
}

是否支持换行读入

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	char b[100];
	cin>>a>>b;  			//hello [回车] world!
	cout<<strlen(a)<<endl; 	        // 5
	cout<<strlen(b)<<endl; 	        // 6
	cout<<a<<endl; 				//hello
	cout<<b<<endl; 				//hello!
	return 0;
}

是否支持string

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
//	char a[100];
//	char b[100];
	string a;
	string b; 
	cin>>a>>b;  			//hello [回车] world!
//	cout<<strlen(a)<<endl; 	// 5
//	cout<<strlen(b)<<endl; 	// 6
	cout<<a.size()<<endl;	// 5
	cout<<b.size()<<endl;	// 6
	cout<<a<<endl; 				//hello
	cout<<b<<endl; 				//hello!
	return 0;
}

3.gets

基本操作

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	gets(a);					//hello 
	cout<<strlen(a)<<endl; 		// 5
	cout<<a<<endl; 				//hello
	return 0;
}

能否读空格

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	gets(a);					//hello world
	cout<<strlen(a)<<endl; 		// 11
	cout<<a<<endl; 				//hello world
 	return 0;
}

多个数组读入

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	char b[100];
	gets(a);					//hello world [回车]
	gets(b);					//ni hao
	cout<<strlen(a)<<endl; 		                // 11
	cout<<strlen(b)<<endl; 		                // 6 
	cout<<a<<endl; 				//hello world
	cout<<b<<endl; 				//ni hao 
 	return 0;
}

是否支持换行读入

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	char a[100];
	char b[100];
	gets(a);					//hello [回车]world [回车]
	gets(b);					//ni hao
	cout<<strlen(a)<<endl; 		// 5 
	cout<<strlen(b)<<endl; 		// 5 
	cout<<a<<endl; 				// hello 
	cout<<b<<endl; 				//world 
 	return 0;
}

是否支持string

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
//	char a[100];
//	char b[100];
	string a;
	string b;
	gets(a);					//hello world [回车]
	gets(b);					//ni hao
//	cout<<strlen(a)<<endl; 		// 5 
//	cout<<strlen(b)<<endl; 		// 5 
	cout<<a.size()<<endl;
	cout<<b.size()<<endl;
	cout<<a<<endl; 				// hello 
	cout<<b<<endl; 				//world 
 	return 0;
}

4.getline

基本用法

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		string a;
		getline(cin,a); 	//hello
		cout<<a.size()<<endl; //5 
		cout<<a<<endl; 		// hello 
	 	return 0;
	}

能否读空格 

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		string a;
		getline(cin,a); 	//hello world
		cout<<a.size()<<endl; //11 
		cout<<a<<endl; 		// hello world;
	 	return 0;
	}

读入多个

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		string a;
		string b;
		getline(cin,a); 	//hello world[回车]
		getline(cin,b);		//ni hao 
		cout<<a.size()<<endl; //11
		cout<<b.size()<<endl; //6
		cout<<a<<endl; 		// hello world;
		cout<<b<<endl;		// ni hao 
	 	return 0;
	}

换行读入数据

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		string a;
		string b;
		getline(cin,a); 	//hello[回车]world[回车]
		getline(cin,b);		//ni hao 
		cout<<a.size()<<endl; //5
		cout<<b.size()<<endl; //5	
		cout<<a<<endl; 		// hello 
		cout<<b<<endl;		// world 
	 	return 0;
	}

5.cin.getline

【函数用法】

接收一个字符串并输出(可含空格)。

【参数详解】

1.cin.getline()函数的完整形式有三个参数:cin.getline(字符数组名,字符个数,结束标志)

    注意:若指定参数“字符个数”为n,则利用cout函数输出时只显示字符数组中的前n-1个字符。

    原因:字符数组的第n个字符是不可见字符'\0'

2.当第三个参数省略时,系统默认为'\0'

3.简单实例:  

  · cin.getline(str,8,'m'),当输入abcdefghijklmn时,输出abcdefg,因为第8位是不可见字符'\0'

  · cin.getline(str,8,'e'),当输入abcdefghijklmn时,输出abcd

基础用法

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		cin.getline(a,5);      //hello
		cout<<a<<endl; 		// hell
	 	return 0;
	}

能否读空格

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		cin.getline(a,5);       //he ll
		cout<<a<<endl; 		// he l
	 	return 0;
	}


换行输入

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		char b[100];
		cin.getline(a,5); //hello
		cin.getline(b,6); //nihao 
		cout<<strlen(a)<<endl; //4
		cout<<strlen(b)<<endl; //0  
		cout<<a<<endl; 		// hell
		cout<<b<<endl; //[空] 
		
	 	return 0;
	}

6.cin.get

基本操作

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		cin.get(a,5); 		//hello
		cout<<strlen(a)<<endl; //4
		cout<<a<<endl; 		// hell
	 	return 0;
	}

能否读入空格

	#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		cin.get(a,5); 		//he llo
		cout<<strlen(a)<<endl; //4
		cout<<a<<endl; 		// he l
	 	return 0;
	}

读入多个

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		char b[100];
		cin.get(a,5); 		//hello
		cin.get(b,5); 		//nihao 
		cout<<strlen(a)<<endl; //4
		cout<<strlen(b)<<endl; //1 
		cout<<a<<endl; 		// hell
	 	return 0;
	}

读回车

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		char b[100];
		cin.get(a,5); 		//hell[回成]o
		cin.get(b,5); 		//nihao 
		cout<<strlen(a)<<endl; //4
		cout<<strlen(b)<<endl; //0 
		cout<<a<<endl; 		// hell
	 	return 0;
	}

7.fgets

基础用法

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		fgets(a,5,stdin); //hello	
		cout<<strlen(a)<<endl; //4
		cout<<a<<endl; 		// hell
	 	return 0;
	}

能否读空格

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		fgets(a,5,stdin); //he llo
		cout<<strlen(a)<<endl; //4
		cout<<a<<endl; 		// he l
	 	return 0;
	}

读入多个

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		char b[100];
		fgets(a,5,stdin); //hello
		fgets(b,5,stdin); //nihao 
		cout<<strlen(a)<<endl; //4
		cout<<strlen(b)<<endl; //2
		cout<<a<<endl; 		// hell
		cout<<b<<endl;		//o 
	 	return 0;
	}

8.scanf

基础用法

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		scanf("%s",a); //hello
		cout<<strlen(a)<<endl; //5
		cout<<a<<endl; 		// hello
	 	return 0;
	}

能否读空格

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		scanf("%s",a); //he llo
		cout<<strlen(a)<<endl; //2
		cout<<a<<endl; 		// he
	 	return 0;
	}

能否读回车

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		scanf("%s",a); //he[回车]llo
		cout<<strlen(a)<<endl; //2
		cout<<a<<endl; 		// he
	 	return 0;
	}

读入多个

#include<iostream>
	#include<cstdio>
	#include<cstring>
	using namespace std;
	int main(){
		char a[100];
		char b[100]; 
		scanf("%s",a); //hello
		scanf("%s",b); //nihao 
		cout<<strlen(a)<<endl; //2
		cout<<strlen(b)<<endl; //5 
		cout<<a<<endl; 		// hello
		cout<<b<<endl;  	//nihao 
	 	return 0;
	}

9.总结

方式写法能否读空格是否支持chars数组是否支持string备注
cin

cin>>a;

cin>>a>>b;

不建议读大数据
gets

gets(a);

gets(b);

考试禁用
getline

getline(cin,a);

getline(cin,b);


cin.getlinecin.geline(a,len,'\0')
cin.getcin.get(a,5)
fgetsfgets(a,5,stdin)
scanfscanf("%s",a)

10.字符数组函数

函数格式函数功能
strcat(s1,s2)将字符串2连接到字符串1后边,返回字符串1的值。
strncat(s1,s2,n)将字符串2前n个字符连接到字符串1后边,返回字符串1的值。
strcpy(s1,s2)将字符串2复制到字符串1,返回字符串1的值
strncpy(s1,s2,n)将字符串2前n个字符复制到字符串1,返回字符串1的值
strcmp(s1,s2)

比较字符串1和字符串2的大小,返回的结果由函数带回

如果字符串1>字符串2,返回一个正整数

如果字符串1=字符换2,返回0

如果字符串1<字符串2,返回一个负整数

strncmp(s1,s2,n)比较字符串1和字符串2的前n个字符,返回值参考strcmp函数
strlen(s1)计算字符串的长度,终止符'\0' 不算在长度范围内
strlwr(s1)
将字符串中大写字母转换成小写字母
strupr(s1)将字符串中小写字母转换成大写字母


扫描二维码推送至手机访问。

版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。

分享给朋友:

相关文章

【算法】前缀和与差分(3)二维数组前缀和

【算法】前缀和与差分(3)二维数组前缀和

0.前言前面的一篇文章,介绍了一维数组的前缀和,这篇文章中,介绍一下二维数组的前缀和1.定义二维数组的前缀和就是按照二维数组求和。公式如下:那二维前缀和中一个f[i][j]表示的意思就是以(1,1)为...

图的访问与存储—临接矩阵

1. 什么是邻接矩阵?邻接矩阵是图的一种最基础的存储表示方法。它使用一个二维数组(即矩阵)来表示图中各个顶点之间的邻接关系。对于一个有 n 个顶点的图,其邻接矩阵是一个 n x n 的方阵,我们通常称...

多重背包问题

一、问题定义有 n 种物品,每种物品有三个属性:重量 weight[i](正整数)价值 value[i](正整数)数量 count[i](正整数,表示...

二维数组的差分

一、基本概念二维数组差分是一种高效处理区间修改操作的数据结构技巧,常用于解决矩阵区域增减问题。差分是前缀和的逆运算,对于二维数组,差分数组 diff[i][j] 表示原数组 a[i][j] 与 a[i...

【STL】二分查找函数(算法)—binary_search

【说明】binary_search() 实现了一个二分查找算法。它会在前两个参数指定范围内搜索等同于第三个参数的元素。指定范围的迭代器必须是正向迭代器而且元素必须可以使用 < 运算符来比较。这个...

【题解】奶牛的回音

【题目描述】奶牛们灰常享受在牛栏中牟叫,因為她们可以听到她们牟声的回音。虽然有时候并不能完全听到完整的回音。Bessie曾经是一个出色的秘书,所以她精确地纪录了所有的牟叫声及其回声。她很好奇到底两个声...