《算法笔记》3.6小节——入门模拟->字符串处理
疑问:
1962 Problem D和1963 Problem E 都是:不加getchar()正确50%,加上后正确100%
PAT B1009 【说反话】20分
pat的C++编译器不让用gets函数?
scanf遇到空格或\n结束; gets遇到\n结束.
#include<cstdio>
#include<cstring>
int main() {
char str[100];
while (gets_s(str)) {
char trans[80][80];
int len = strlen(str);
int num = 0, j = 0;
for (int i = 0; i <len; i++) {
if (str[i] != ' ') trans[num][j++] = str[i];
else {
//每个单词结束时要在结尾加结束字符
trans[num][j] = '\0';
num++; j = 0;
}
}
trans[num][j] = '\0';
for (int i = num; i >=0; i--) {
printf("%s",trans[i]);
if (i != 0)printf(" ");
else printf("\n");
}
}
return 0;
}
1785 Problem A 字符串连接
#include<cstdio>
#include<cstring>
int main() {
char a[200], b[100];
while (scanf("%s %s", &a, &b) != EOF) {
int lena = 0 , lenb = 0;
while (a[lena] != '\0') lena++;
while (b[lenb] != '\0') lenb++;
//注意字符串末尾的'\0'
for (int i = 0; i < lenb; i++) {
a[lena + i] = b[i];
}
lena += lenb;
a[lena] = '\0';
printf("%s\n", a);
//或者把b字符串的末尾'\0'也复制到a的末尾,即
//for (int i = 0; i <= lenb; i++) {
// a[lena + i] = b[i];
//}
//printf("%s\n", a);
}
return 0;
}
1805 Problem B 首字母大写
vs17用不了gets(),要用gets_s()
#include<cstdio>
#include<cstring>
int main() {
//通过gets读入一整行字符
char str[100];
while (gets_s(str) ) {
if ((str[0] >= 97 && str[0] <= 122)) str[0] -= 32;
for (int i = 1; i < strlen(str); i++) {
//不确定优先级的一定要加括号,不加括号容易错
if((str[i-1]==' ' ||str[i-1]=='\t'|| str[i-1]=='\r' || str[i-1]=='\n') &&(str[i]>=97 && str[i]<=122))
str[i]-=32;
}
puts(str);
}
return 0;
}
或者用flag记录str[i-1]是否是特殊符号
#include<cstdio>
#include<cstring>
int main() {
//通过gets读入一整行字符
char str[100];
while (gets_s(str) ) {
int flag = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n')
flag = 0;
else {
if (flag == 0 && (str[i] >= 97 && str[i] <= 122))
str[i] -= 32;
flag = 1;
}
}
puts(str);
}
return 0;
}
1808 Problem C 字符串的查找删除
半成品
#include<cstdio>
#include<cstring>
/*
int KMP(char S[], char T[], int next[], int pos) {
int i = pos;
int j = 0;
for (int i = 0; i < strlen(S); i++) {
if (S[i] >= 'A'&& S[i] <= 'Z') S[i] = S[i] + 32;
}
while (i <= strlen(S) && j <= strlen(T)) {
if (j == 0 || S[i] == T[j]) {
i++; j++;
}
else {
j = next[j];
}
}
if (j > 0) return i - strlen(T);
else return -1;
}
void get_next(char T[], int next[]) {
int i = 0, j = 0;
next[1] = 0;
while (i < strlen(T)) {
if (j == 0 || T[i] == T[j]) {
next[i] = j;
i++; j++;
}
else {
j = next[j];
}
}
}
*/
int main() {
char del[20];
scanf("%s", &del);
for (int i = 0; i < strlen(del); i++) {
if (del[i] >= 'A'&&del[i] <= 'Z') del[i] = del[i] + 32;
}
int lendel = strlen(del);
//注意大写小写In、IN、iN、in都要删除
//第一步删除所有空格
//第二步将字符串先做统一化处理,用KMP算法找出匹配位置,删除或打印时略过
char str[50];
while (scanf("%s", &str) != EOF) {
int temp = 0,len = 0;
for (len = 0; len < strlen(str)-temp; len++) {
if (str[len] == ' ') {
temp++;
str[len] = str[len + temp];
}
}
str[len] = '\0';
int num = 0;
if (num == -1) printf("%s", str);
else {
for (int i = 0; i <len; i++) {
if (i < num || i >= (num + strlen(del)))
printf("%c", str[i]);
}
printf("\n");
}
}
return 0;
}
1962 Problem D 单词替换
最后一个getchar(),否则要输两次Ctrl+Z才能结束
也避免多余的符号被gets进s
#include <stdio.h>
#include <string.h>
int main() {
char s[120], a[120], b[120];
while (gets_s(s)) {
char arr[120][120];
int num = 0, j = 0;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == ' ') {
arr[num][j] = '\0';
num++; j = 0;
}
else {
arr[num][j++] = s[i];
}
}
arr[num][j] = '\0';
scanf("%s", &a);
scanf("%s", &b);
for (int i = 0; i <= num; i++) {
if (strcmp(a, arr[i]) == 0) strcpy(arr[i], b);
printf("%s", arr[i]);
if(i== num) printf("\n");
else printf(" ");
}
//memset(s, '\0', sizeof(s));
//memset(a, '\0', sizeof(a));
//memset(b, '\0', sizeof(b));
getchar();
}
return 0;
}
1963 Problem E 字符串去特定字符
%s通过空格和换行来识别一个字符串的结束,%c是可以匹配空格的
另开数组,用空间换时间
#include <stdio.h>
#include <string.h>
int main() {
char s[400];
char c;
while (gets_s(s)){
scanf("%c", &c);
char temp[400];
int len = 0;
for (int i = 0; i < strlen(s); i++) {
//for (int i = 0; s[i] != '\0'; i++)
if (s[i] != c)
temp[len++] = s[i];
}
temp[len] = '\0';
printf("%s\n", temp);
memset(temp, '\0', sizeof(temp));
getchar();
}
return 0;
}
1967 Problem F 数组逆置
#include <stdio.h>
#include <string.h>
#include<algorithm>
using namespace std;
int main() {
char str[210];
while (gets(str)) {
int len = strlen(str);
for (int i = 0; i < len/2; i++) {
swap(str[i], str[len - 1 - i]);
}
printf("%s\n", str);
}
return 0;
}
2025 Problem G 比较字符串
#include <stdio.h>
#include <string.h>
int main() {
int m = 0;
scanf("%d", &m);
while(m--) {
char a[60], b[60];
scanf("%s %s", &a, &b);
if (strlen(a) > strlen(b)) printf("%s is longer than %s\n",a,b);
else if (strlen(a) == strlen(b)) printf("%s is equal long to %s\n",a,b);
else printf("%s is shorter than %s\n",a,b);
}
return 0;
}
2064 Problem H 编排字符串
#include <stdio.h>
#include <string.h>
int main() {
char a[100][25];
int m = 0;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%s", a[i]);
for (int j = i; j >= 0&&i-j+1<=4; j--) {
printf("%d=%s", i - j + 1, a[j]);
if (j != 0 || (j == 0 && i == 0)) printf(" ");
}
printf("\n");
}
return 0;
}
5901 Problem I 【字符串】回文串
#include<cstdio>
#include<cstring>
int main() {
char str[260];
while (scanf("%s", &str) != EOF) {
bool flag = true;
for (int i = 0; i < strlen(str)/2; i++) {
if (str[i] != str[strlen(str) - 1 - i]) {
flag = false;
break;
}
}
if (flag == true) printf("YES\n");
else printf("NO\n");
}
return 0;
}