题目来源
地址:https://pintia.cn/problem-sets/994805046380707840/exam/problems/1111914599412858885
错误信息
代码内容
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int isBDFH(char ch) //标点符号或空格判断函数
{
if ((ch != ' ' && ch != '\0' && !(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z')) || (ch == ' '))
return 1;
return 0;
}
int kongge(char str[]) //字符串空格处理函数
{
int len = strlen(str);
for (int i = 0; i < len; i++) //处理字符串开头空格
{
if (str[i] == ' ')
str[i] = '\0';
else
break;
}
len = qingli(str, len);
for (int i = len - 1; i >= 0; i--) //处理字符串末尾空格
{
if (str[i] == ' ')
str[i] = '\0';
else
break;
}
len = qingli(str, len);
for (int i = 0; i < len; i++) //处理字符串标点前空格
{
if (isBDFH(str[i]))
{
if (i - 1 >= 0 && str[i - 1] == ' ') //i-1<0则下标不存在,&&判断结束,后者数组不会越界访问
{
for (int j = i - 1; j >= 0; j--)
{
if (str[j] == ' ')
str[j] = '\0';
else
break;
}
}
}
}
len = qingli(str, len);
for (int i = 0; i < len; i++) //处理字符串中间的多个空格
{
if (str[i] != ' ')
{
if (i + 1 <= len - 1 && str[i + 1] == ' ') //找到一个非空格字符,它的下一个字符是空格
{
int count = 0;
int j = i + 1;
for (; j < len; j++)
{
if (str[j] == ' ')
count++;
else
break;
}
for (int k = 1; k < count; k++) //删去其它空格,保留一个空格
str[i + k] = '\0';
i = j - 1; //更新i
}
}
}
len = qingli(str, len);
return len;
}
int qingli(char str[], int len) //字符串空字符清理函数
{
char dest[10000] = { '\0' };
int count = 0;
for (int i = 0; i < len; i++)
{
if (str[i] != '\0')
{
dest[count++] = str[i];
}
}
strcpy(str, dest);
len = count;
return len;
}
int duli(char str[]) //字符串的独立子字符串删除函数
{
int len = strlen(str);
int res = 0;
char sta1[100] = { "can you" };
char trans1[100] = { "A can" };
for (int i = 0; i <= len - 1; i++)
{
if (str[i] == 'c' && len - i >= 7)
{
res = 1;
for (int j = 1; j <= 6 && res == 1; j++)
{
if (str[i + j] != sta1[j])
res = 0;
}
if (res == 1) //匹配到目标字符串
{
if (i - 1 < 0 && i + 7 > len - 1) //前,后均是边界
{
strcpy(str, "A can");
}
else if (i - 1 < 0 && isBDFH(str[i + 7])) //前是边界,后满足条件
{
for (int j = 0; j <= 6; j++)
str[i + j] = trans1[j];
}
else if (i + 7 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
{
for (int j = 0; j <= 6; j++)
str[i + j] = trans1[j];
}
else if ((i - 1 >= 0 && i + 7 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 7]))) //前后均非边界且均满足条件
{
for (int j = 0; j <= 6; j++)
str[i + j] = trans1[j];
}
i = i + 6;//更新i,之后会i++
}
}
}
len = qingli(str, len);
strcpy(sta1, "could you");
strcpy(trans1, "A could");
for (int i = 0; i <= len - 1; i++)
{
if (str[i] == 'c' && len - i >= 9)
{
res = 1;
for (int j = 1; j <= 8 && res == 1; j++)
{
if (str[i + j] != sta1[j])
res = 0;
}
if (res == 1) //匹配到目标字符串
{
if (i - 1 < 0 && i + 9 > len - 1) //前,后均是边界
{
strcpy(str, "A could");
}
else if (i - 1 < 0 && isBDFH(str[i + 9])) //前是边界,后满足条件
{
for (int j = 0; j <= 8; j++)
str[i + j] = trans1[j];
}
else if (i + 9 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
{
for (int j = 0; j <= 8; j++)
str[i + j] = trans1[j];
}
else if ((i - 1 >= 0 && i + 9 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 9]))) //前后均非边界且均满足条件
{
for (int j = 0; j <= 8; j++)
str[i + j] = trans1[j];
}
i = i + 8;//更新i,之后会i++
}
}
}
len = qingli(str, len);
strcpy(sta1, "me");
strcpy(trans1, "you");
for (int i = 0; i <= len - 1; i++)
{
if (str[i] == 'm' && len - i >= 2)
{
res = 1;
for (int j = 1; j <= 1 && res == 1; j++)
{
if (str[i + j] != sta1[j])
res = 0;
}
if (res == 1) //匹配到目标字符串
{
if (i - 1 < 0 && i + 2 > len - 1) //前,后均是边界
{
strcpy(str, "you");
}
else if (i - 1 < 0 && isBDFH(str[i + 2])) //前是边界,后满足条件
{
for (int j = len - 1; j >= i + 2; j--) //me后面的字符后移1格
str[j + 1] = str[j];
for (int j = i, k = 0; j <= i + 2; j++, k++) //对应位置填充字符
str[j] = trans1[k];
}
else if (i + 2 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
{
for (int j = len - 1; j >= i + 2; j--)
str[j + 1] = str[j];
for (int j = i, k = 0; j <= i + 2; j++, k++)
str[j] = trans1[k];
}
else if ((i - 1 >= 0 && i + 2 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 2]))) //前后均非边界且均满足条件
{
for (int j = len - 1; j >= i + 2; j--)
str[j + 1] = str[j];
for (int j = i, k = 0; j <= i + 2; j++, k++)
str[j] = trans1[k];
}
i = i + 2;
len += 1;
}
}
}
len = qingli(str, len);
strcpy(sta1, "I");
strcpy(trans1, "you");
for (int i = 0; i <= len - 1; i++)
{
if (str[i] == 'I' && len - i >= 1)
{
res = 1;
if (res == 1) //匹配到目标字符
{
if (i - 1 < 0 && i + 1 > len - 1) //前,后均是边界
{
strcpy(str, "you");
}
else if (i - 1 < 0 && isBDFH(str[i + 1])) //前是边界,后满足条件
{
for (int j = len - 1; j >= i + 1; j--) //I后面的字符后移2格
str[j + 2] = str[j];
for (int j = i, k = 0; j <= i + 2; j++, k++) //对应位置填充字符
str[j] = trans1[k];
}
else if (i + 1 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
{
for (int j = len - 1; j >= i + 1; j--)
str[j + 2] = str[j];
for (int j = i, k = 0; j <= i + 2; j++, k++)
str[j] = trans1[k];
}
else if ((i - 1 >= 0 && i + 1 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 1]))) //前后均非边界且均满足条件
{
for (int j = len - 1; j >= i + 1; j--)
str[j + 2] = str[j];
for (int j = i, k = 0; j <= i + 2; j++, k++)
str[j] = trans1[k];
}
i = i + 2;
len += 2;
}
}
}
len = qingli(str, len);
return len;
}
int main()
{
int n;
scanf("%d", &n);
getchar();
char str[10000] = { '\0' };
for (int i = 0; i < n; i++)
{
gets(str);
puts(str);
int len = strlen(str);
for (int j = 0; j < len; j++)
{
if (str[j] >= 'A' && str[j] <= 'Z' && str[j] != 'I')
str[j] += 32;
else if (str[j] == '?')
str[j] = '!';
}
kongge(str);
len = duli(str);
for (int j = 0; j < len; j++)
{
if (str[j] == 'A')
str[j] = 'I';
}
printf("AI: %s\n", str);
}
return 0;
}
备注
题目细节很多,代码写了很长,想不清楚是哪个地方没有考虑到。求解答,万分感谢!