字符串的声明

声明30个字符串
string[30]
可以用a=string[x].size( )求得字符串x的长度

#include<bits/stdc++.h>
using namespace std;
string tr[30]; //声明30个字符串

//输入n个字符串 再输入一个字符串ch
//挨个进行比较  如果它们的第一个字符相同  输出yes
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>tr[i];
	char ch;
	cin>>ch;
	for(int i=1;i<=n;i++){
		cout<<tr[i][0]<<" ";//输出每个字符串的第一个字符
		if(tr[i][0]==ch) cout<<"yes";
		cout<<endl;
	} 
	return 0;
}

输入
5
at
touch
cheat
choose
tact
a


输出
a
a yes
t
c
c
t

char s[ ] 只能声明一个字符串
char s[i]表示字符串的第i个的对应字符

#include<bits/stdc++.h>
using namespace std;
char tr[30];

int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>tr[i];
	char ch;
	cin>>ch;
	for(int i=1;i<=n;i++){
		cout<<tr[i]<<" ";
		if(tr[i]==ch) cout<<"yes";
		cout<<endl;
	} 
	return 0;
}

输入
5
at
touch

输出
a
t
t
o
u//它识别了前5个字符 即 attou 为tr[i]存储的内容 而 ch就是剩下的ch

char 也可以实现声明30个组
char[ ][ ]不过一定要打两个括号
但是不能像string一样求长度 因为第二个括号规定了长度 很不方便 运算会超时

#include<bits/stdc++.h>
using namespace std;
const int N=110;
struct node{
	int x,y;
}c[N];
char fz[N][N],stand[ ]="yizhong";
int vis[N][N];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%s",fz[i]);
	for(int i=0;i<n;i++) {
		cout<<fz[i]<<endl;//输出整个字符串 若改成 cout<<fz[i][0]则可实现输出第一个字符
	}
	return 0;
}

输入
8
qyizhong
gydthkjy
nwidghji
orbzsfgz
hhgrhwth
zzzzzozo
iwdfrgng
yyyygggg

输出

qyizhong
gydthkjy
nwidghji
orbzsfgz
hhgrhwth
zzzzzozo
iwdfrgng
yyyygggg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值