P5733 【深基6.例1】自动修正 - 洛谷
小写字母 - 32 = 大写字母
大写字母 + 32 = 小写字母
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N] = { 0 };
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a;
int i = 0;
while (a[i] != '\0')
{
if (a[i] >= 'a' && a[i] <= 'z')
{
a[i] -= 32;
}
cout << a[i];
i++;
}
cout << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N] = { 0 };
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a;
int len = strlen(a);
int i = 0;
for (i = 0; i < len; i++)
{
if (a[i] >= 'a' && a[i] <= 'z')
{
a[i] -= 32;
}
}
cout << a << endl;
return 0;
}
这⾥再给⼤家介绍两个函数: islower 和 tolower ,需要的头⽂件是 <cctype>
字符分类函数和字符转换函数:https://legacy.cplusplus.com/reference/cctype/
int islower ( int c ); //判断字符是否是⼩写字⺟
int tolower ( int c ); //转换成⼩写字⺟
islower 是C/C++中提供的⼀个判断字符是否是⼩写字⺟的函数,如果参数 c 是⼩写字⺟,函数返回⼀个⾮0的数字,如果不是⼩写字⺟,函数返回0,其实还有⼀个函数是 isupper ,是判断⼤写字⺟的。
tolower 是C/C++中提供的⼀个将参数 c 从⼤写字⺟转化成⼩写字⺟的函数,通过返回值返回转换后的⼩写字⺟。如果 c 本⾝就是⼩写字⺟,则什么都不发⽣。还有⼀个函数是 toupper ,是⼩写字⺟转换成⼤写的。
#include <iostream>
#include <cctype>
using namespace std;
const int N = 110;
char s[N];
int main()
{
cin >> s;
for(int i = 0; s[i] != '\0'; i++)
{
if(islower(s[i]))
{
s[i] = toupper(s[i]);
}
}
cout << s <<endl;
return 0;
}
B2109 统计数字字符个数 - 洛谷
#include <bits/stdc++.h>
using namespace std;
const int N = 265;
char a[N] = {0};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
fgets(a, 265, stdin); //会一直读到\n,将\n放入到数组末尾,再加上\0
int i = 0;
int cnt = 0;
while (a[i] != '\n')
{
if (a[i] >= '0' && a[i] <= '9')
cnt++;
i++;
}
cout << cnt << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 265;
char a[N] = {0};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
scanf("%[^\n]s", a); //不会读取\n,会放上\0
int i = 0;
int cnt = 0;
while (a[i] != '\0')
{
if (a[i] >= '0' && a[i] <= '9')
cnt++;
i++;
}
cout << cnt << endl;
return 0;
}
判断⼀个字符是否是数字字符有⼀个函数是 isdigit ,可以直接使⽤。
int isdigit ( int c );
如果参数 c 是数字字符,则返回⾮ 0 的值,如果不是数字字符,则返回 0 。
#include <iostream>
#include <cctype>
using namespace std;
const int N = 266;
char arr[N];
int main()
{
//下⾯这种读取⽅式遇到\n就停⽌,不会讲\n存⼊arr,会⾃动在末尾存放\0
scanf("%[^\n]s", arr);
int i = 0;
int c = 0;
while (arr[i] != '\0') //这⾥判断是否等于\0,来觉得是否结束
{
if (isdigit(arr[i]))
c++;
i++;
}
cout << c << endl;
return 0;
}
整理药名
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
char a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
while (n--)
{
cin >> a;
if (islower(a[0]))
a[0] = toupper(a[0]);
int i = 1;
while (a[i] != '\0')
{
if (isupper(a[i]))
a[i] = tolower(a[i]);
i++;
}
cout << a << endl;
}
return 0;
}
B2111 基因相关性 - 洛谷
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
char a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
double n = 0;
cin >> n;
cin >> a;
int i = 0;
char t = 0;
int cnt = 0;
while (a[i] != '\0')
{
cin >> t;
if (a[i] == t)
cnt++;
i++;
}
if (cnt * 1.0 / strlen(a) >= n)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
B2113 输出亲朋字符串 - 洛谷
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a; //a的末尾是\0
int i = 0;
while (a[i+1])
{
char tmp = a[i] + a[i+1];
cout << tmp;
i++;
}
cout << (char)(a[i] + a[0]) << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a; //a的末尾是\0
int i = 0;
int len = strlen(a);
while (a[i])
{
char tmp = a[i] + a[(i+1) % len];
cout << tmp;
i++;
}
return 0;
}
- 这类题⽬就是精确的控制下标,防⽌越界,算准下标
- cout 在打印数据的时候是需要明确知道打印数据的类型的,⽅法1中
str[i] + str[0]
算的结果直接打印就被编译器当做整数打印了,所以我们做了 (char) 的强制类型转换。
B2118 验证子串 - 洛谷
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
char a[N];
char b[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a >> b;
if (strstr(a, b))
cout << b << " is substring of " << a << endl;
else if (strstr(b, a))
cout << a << " is substring of " << b << endl;
else
cout << "No substring" << endl;
return 0;
}
这个题⽬使⽤了 strstr 函数,这个函数需要 <cstring>
的头⽂件。
const char * strstr ( const char * str1, const char * str2 );
这个函数可以查找str1字符串中str2字符串第⼀次出现的位置,如果能找到就返回第⼀次出现的地址,如果找不到则返回 NULL , NULL 的本质是 0 。
B2110 找第一个只出现一次的字符 - 洛谷
暴力统计
#include <bits/stdc++.h>
using namespace std;
const int N = 1110;
char a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a;
int i = 0;
while (a[i])
{
int cnt = 0;
int j = 0;
while (a[j])
{
if (a[i] == a[j])
cnt++;
j++;
}
if (cnt == 1)
{
cout << a[i];
break;
}
i++;
}
if (a[i] == 0)
cout << "no" << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1110;
char a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a;
int i = 0;
int flag = 0;
while (a[i])
{
int cnt = 0;
int j = 0;
while (a[j])
{
if (a[i] == a[j])
cnt++;
j++;
}
if (cnt == 1)
{
cout << a[i];
flag = 1;
break;
}
i++;
}
if (flag == 0)
cout << "no" << endl;
return 0;
}
哈希
题⽬说字符串中只有⼩写字⺟,⼩写字⺟的ASCII值的范围是:97122,根据前⾯我们学习的知识,我们知道在C和C++中每个字符都有ASCII值,标准的ASCII码表中有128个字符,ASCII值的范围是0127.
所以我们创建⼀个128元素的整型数组,下标分别是0~127,下标正好和字符的ASCII值范围对应,那么整型的数组的⼀个元素就为⼀个字符计数就可以。每读取⼀个字符,根据字符的ASCII值将数组中下标为字符ASCII值的元素值+1,相当于统计这个字符出现的次数。
#include <bits/stdc++.h>
using namespace std;
const int N = 1110;
char a[N];
int n[128];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a;
int i = 0;
while (a[i])
{
n[a[i]]++;
i++;
}
i = 0;
while (a[i])
{
if(n[a[i]] == 1)
{
cout << a[i] << endl;
break;
}
i++;
}
if (a[i] == 0)
cout << "no" << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1110;
char a[N];
int n[26];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a;
int i = 0;
while (a[i])
{
n[a[i] - 'a']++;
i++;
}
i = 0;
while (a[i])
{
if(n[a[i] - 'a'] == 1)
{
cout << a[i] << endl;
break;
}
i++;
}
if (a[i] == 0)
cout << "no" << endl;
return 0;
}