声明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