课后作业:字符数组
改错题
任务描述
本关任务:输入一个字符串(少于80个字符),把字符串中所有的数字字符转换为整数,去掉其他字符。
测试样例
平台会对你编写的代码进行测试:
- 测试输入:
pro56gr am93 m4in g - 预期输出:
56934
参考代码
#include <iostream>
using namespace std;
int main(){
int i, j, n = 0;
char str[80];
i = 0;
while((str[i]=cin.get())!='\n')
i++;
str[i] = '\0';
for(j = 0; str[j]!='\0'; j++){
if(str[j]>='0' && str[j]<='9')
n = n*10 + str[j]-'0';
}
cout << n << endl;
return 0;
}
查找字符
任务描述
本关任务:编写一个程序,首先输入一个以回车键结束的字符串(少于80个字符),接着再输入一个字符,查找该字符在字符串中是否存在。如果找到,则输出该字符串在字符串中所对应的最大下标(下标从0开始);否则输出“Not Found”。
可参考如下两个案例便于理解:
测试样例
- 案例一
- 输入:
program design g - 输出:
Index=12
- 输入:
- 案例二
- 输入:
program design k - 输出:
Not Found
- 输入:
参考代码
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char s[100], t;
cin.getline(s, 81); cin.get(t);
bool f=0;
for (int i=strlen(s)-1;i>=0;i--) if (s[i]==t) {
cout << "Index=" << i << endl;
f=1; break;
}
if (!f) cout << "Not Found" << endl;
return 0;
}
字符数统计
任务描述
本关任务:编写一个程序,从键盘上输入一篇英文文章。首先输入英文文章的行数n(1≤n≤10),接着依次输入n行内容(每行少于80个字符)。要求统计出其中的英文字母(不区分大小写)、数字和其他非空白字符的个数。
测试样例
- 测试输入:
2 21st century is the century of technology. Nowadays, technology is everywhere around us. - 案例输出:
英文字母:71 数字:2 其他字符:3 - 测试输入:
6 We focus on essay generation, which is a challenging task that generates a paragraph-level text with multiple topics. Progress towards understanding different topics and expressing diversity in this task requires more powerful generators and richer training and evaluation resources. - 案例输出:
英文字母:241 数字:0 其他字符:4
参考代码
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int n;
cin>>n;
getchar();
int num = 0;
int alpha = 0;
int blank = 0;
int ascll = 0;
for (int i=0;i<n;i++){
char str[80];
scanf("%[^\n]",&str);
getchar();
for (int j=0;j<strlen(str);j++){
if(str[j]>='0' && str[j]<='9'){
num+=1;
}else if ((str[j]>='a' && str[j]<='z') || (str[j]>='A' && str[j]<='Z')){
alpha+=1;
}else {
ascll+=1;
}
if (str[j]==' ') blank+=1;
}
}
cout<<"英文字母:"<<alpha<<endl<<"数字:"<<num<<endl<<"其他字符:"<<ascll-blank;
return 0;
}
查找回文字符串
任务描述
本关任务:编写一个程序,寻找一篇英文文章中最长的回文字符串。
回文字符串是具有回文特性的字符串:即该字符串从左向右读,与从右向左读都一样。
输入文件不会超过500字符。这个文件可能一行或多行,但是每行都不超过80个字符(不包括最后的换行符)。在寻找回文时只考虑字母 ‘A’ - ‘Z’ 和 ‘a’ - ‘z’ ,忽略其他字符(例如:标点符号,空格等)。
输出的第一行应该包括找到的最长的回文的长度。下一行或几行应该包括这个回文的原文(没有除去标点符号,空格等),把这个回文输出到一行或多行(如果回文中包括换行符)。如果有多个回文长度都等于最大值,输出最前面出现的那一个。
测试样例
- 测试输入:
Confucius say: Madam, I'm Adam. - 预期输出:
11 Madam, I'm Adam
参考代码
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[500];
char str_alpha[500];
int str_index[500];
int t=0;
for (;(str[t++]=cin.get())!=-1;) ; str[t]=0;
int flag = 0;
for (int i=0;i<strlen(str);i++){
if ((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')){
char tmp = str[i];
if (str[i]>='A' && str[i]<='Z'){
tmp = tmp-'A'+'a';
}
str_alpha[flag] = tmp;
str_index[flag] = i;
flag+=1;
}
}
int n = flag;
bool dp[n][n];
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
dp[i][j] = false;
}
}
int res[2] = {-1,-1};
for (int i=0;i<n;i++){
for (int j=i;j>=0;j--){
if (str_alpha[i] == str_alpha[j] && ((i-j<2) || dp[j+1][i-1])){
dp[j][i] = true;
if (res[0] == -1 || ((i-j+1)>(res[1]-res[0]))){
res[0] = j;
res[1] = i+1;
}
}
}
}
cout<<res[1]-res[0]<<endl;
for (int i=str_index[res[0]];i<=str_index[res[1]-1];i++){
cout<<str[i];
}
return 0;
}