字符数组 char
c语言中没有string只有char,c++中两者都有
初始化字符数组
char x与char y[]
这里的x是一个字符,y则是一个字符数组也就是字符串了
以下是一些正确和错误的初始化方式
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 2e5 + 10,inf = 0x3f3f3f3f;
int main()
{
IOS;
char x = 'a';
char xx = "a";
char xxx = "aa";
char y[] = 'a';
char yy[] = "a";
}
一维字符数组:存放一个字符串
二维字符数组:存放多个字符串,第一维是字符串的数量,第二维是每一个字符串的长度(形如二维数组)
注意:字符数组的长度是 实际内容 + '\0'
例如 char s[] = "bilibili"看似是s[0 ~ 7]但实际上还有一个s[8] = '\0',这一位不会输出出来但是确实存在
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 2e5 + 10,inf = 0x3f3f3f3f;
int main()
{
IOS;
char x[] = "bilibili";
char y = 'a';
cout<<sizeof(x)<<" "<<sizeof(y);
}
初始化字符数组的时候可以在[]中填入字符数组的长度,但是初始化不能超过该长度(注意\0),也可以省略填入的数字,这样一来该字符数组会根据你的初始化内容开出相应的空间,但是必须得输入内容。
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 2e5 + 10,inf = 0x3f3f3f3f;
char c[5] = "1234";
char x[] = "sduchbnweihdoweijdowejd d";
int main()
{
IOS;
cout<<c<<"\n"<<x;
}
字符数组可以用
char a[100] = "123";
这样的方式初始化但是不能用这种方式在使用中对其进行赋值,若想要在初始化之后对该字符数组重新赋值只能对其中元素一一循环赋值
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 2e5 + 10,inf = 0x3f3f3f3f;
char a[100] = "123";
int main()
{
IOS;
a = "124";
cout<<a;
}
输入输出字符数组
可以使用scanf/printf或者cin/cout,但是存在一个问题就是无法读入空格,只能输入一个整段的串.
所以如果遇到可能会出现空格的输入情况时基本上会选择string(string也不能靠cin/scanf输入空格)
但也有函数可以帮助在字符数组中输入空格
1.fgets
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 2e5 + 10,inf = 0x3f3f3f3f;
char a[100];
int main()
{
IOS;
fgets(a , 100 , stdin);
cout<<a;
}
2.getchar(只读入缓存区的一个字符)
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 2e5 + 10,inf = 0x3f3f3f3f;
char a[100];
int main()
{
IOS;
char c;
int i = 0;
while((c = getchar()) != '\n')
{
a[i++] = c;
}
cout<<a;
}
字符数组相关函数
1.
tolower(将字符串中的大写字母换成小写字母)
toupper(将字符串中的小写字母换成大写字母)
例题P5733 【深基6.例1】自动修正 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 105,inf = 0x3f3f3f3f;
char a[N];
int main()
{
IOS;
char c;
int i = 0;
while((c = getchar()) != '\n')
{
if(c >= 'a' && c <= 'z')
{
c = c - 'a' + 'A';
}
a[i++] = c;
}
cout<<a;
}
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 105,inf = 0x3f3f3f3f;
char a[N];
int main()
{
IOS;
char c;
int i = 0;
while((c = getchar()) != '\n')
{
a[i++] = toupper(c);
}
cout<<a;
}
2.
strcat(字符数组1,字符数组2)
把字符数组2连到字符数组1后面
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 105,inf = 0x3f3f3f3f;
char a[] = "luluxiu",b[] = "C.C.";
int main()
{
IOS;
cout<<a<<" "<<b<<"\n\n\n";
strcat(a , b);
cout<<a<<"\n";
}
3.strcpy(str1 , str2)
将str2拷贝到str1中
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 105,inf = 0x3f3f3f3f;
char a[] = "123456",b[] = "789";
int main()
{
IOS;
cout<<a<<" "<<b<<"\n\n\n";
strcat(a , b);
cout<<a<<"\n"<<b;
}
4.strlen与sizeof
strlen返回字符串的长度(不包括\0)
sizeof返回字符串的长度(包括\0)
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 105,inf = 0x3f3f3f3f;
char a[] = "luluxiu";
int main()
{
IOS;
cout<<strlen(a)<<"\n"<<sizeof(a);
}
P5015 [NOIP2018 普及组] 标题统计 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 105,inf = 0x3f3f3f3f;
char s[N];
int main()
{
IOS;
char c;
int i = 0;
while((c = getchar()) != '\n')
{
s[i++] = c;
}
int ans = 0;
for(int i = 0 ; i < strlen(s) ; i++)
{
if(s[i] != ' ')
{
ans++;
}
}
cout<<ans;
}
P5734 【深基6.例6】文字处理软件 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 10500,inf = 0x3f3f3f3f;
char a[N],b[N],c[N],d[N];
int main()
{
IOS;
int q;
cin>>q>>a;
while(q--)
{
int op;
cin>>op;
if(op == 1)
{
cin>>b;
strcat(a , b);
cout<<a<<"\n";
memset(b , 0 , sizeof b);
}else if(op == 2)
{
int l,r;
cin>>l>>r;
for(int i = l ; i < l + r ; i++)
{
b[i - l] = a[i];
}
strcpy(a , b);
cout<<a<<"\n";
memset(b , 0 , sizeof b);
}else if(op == 3)
{
int x;
cin>>x>>b;
for(int i = 0 ; i < x ; i++)
{
c[i] = a[i];
}
strcat(c , b);
for(int i = x ; i < strlen(a) ; i++)
{
d[i - x] = a[i];
}
strcat(c , d);
strcpy(a , c);
cout<<a<<"\n";
memset(c , 0 , sizeof c);
memset(d , 0 , sizeof d);
}else
{
cin>>b;
int f = -1;
bool flag = 0;
for(int i = 0 ; i < strlen(a) ; i++)
{
if(a[i] == b[0])
{
f = i;
for(int j = 0 ; j < strlen(b) ; j++)
{
if(a[i + j] != b[j])
{
f = -1;
break;
}
}
if(f != -1)
{
flag = 1;
cout<<f<<"\n";
break;
}else
{
continue;
}
}
}
if(!flag)
{
cout<<-1<<"\n";
}
memset(b , 0 , sizeof b);
}
}
}
P1200 [USACO1.1] 你的飞碟在这儿 Your Ride Is Here - 洛谷 | 计算机科学教育新生态
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 1e5 + 10,inf = 0x3f3f3f3f,mod = 47;
char s1[10],s2[10];
int main()
{
IOS;
int a = 1,b = 1;
fgets(s1 , 10 , stdin);
fgets(s2 , 10 , stdin);
for(int i = 0 ; i < strlen(s1) ; i++)
{
int c = s1[i] - 'A' + 1;
a = (a * c) % mod;
}
for(int i = 0 ; i < strlen(s2) ; i++)
{
int c = s2[i] - 'A' + 1;
b = (b * c) % mod;
}
if(a == b)
{
cout<<"GO";
}else
{
cout<<"STAY";
}
}
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 1e5 + 10,inf = 0x3f3f3f3f,mod = 47;
int n;
char s[300];
int main()
{
IOS;
cin>>n>>s;
int cnt = 0,ans = 0;
for(int i = 0 ; i + 1 < n ; i++)
{
if(s[i] == 'V' && s[i + 1] == 'K')
{
ans++;
s[i] = '.',s[i + 1] = '.';
}
}
for(int i = 0 ; i < n ; i++)
{
if(s[i] == 'V' && s[i + 1] != '.' && i + 1 < n)
{
cnt = 1;
}
if(s[i] == 'K' && s[i - 1] != '.' && i != 0)
{
cnt = 1;
}
}
cout<<ans + cnt;
}
#define yyy cout<<"Yes"<<"\n"
#define nnn cout<<"No"<<"\n"
#define x first
#define y second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
const int N = 1e5 + 10,inf = 0x3f3f3f3f;
char s[200];
int cnt[50];
char mp[50][500];
int main()
{
IOS;
int n = 0;
for(int i = 1 ; i <= 4 ; i++)
{
fgets(s , 100 , stdin);
for(int j = 0 ; j < strlen(s) ; j++)
{
if(s[j] < 'A' || s[j] > 'Z')
{
continue;
}
int k = s[j] - 'A';
cnt[k]++;
n = max(n , cnt[k]);
}
}
for(int i = 1 ; i <= n ; i++)
{
for(int j = 0 ; j < 26 ; j++)
{
if(cnt[j])
{
cnt[j]--;
mp[i][j] = '*';
}else
{
mp[i][j] = ' ';
}
}
}
for(int i = n ; i >= 1 ; i--)
{
for(int j = 0 ; j < 26 ; j++)
{
cout<<mp[i][j]<<" ";
}
cout<<"\n";
}
cout<<"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
}