题目链接
注意点:
1.每行读取结束后还要再用getchar();读取换行符,否则会出错;
2.间隔符可能为空格,因此要用getline读取字符串;
3.读取每个单词时,最后一个单词末尾如果没有间隔符,是读取不到的,因此还要考虑i == str.size() - 1的情况,把单词添加上。
代码
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 10010;
int main(){
int Letter[26][7][5];
for(int i = 0; i < 26; i++){
for(int j = 0; j < 7; j++){
for(int k = 0; k < 5; k++){
char l;
scanf("%c", &l);
if(l == '.'){
Letter[i][j][k] = 0;
}else{
Letter[i][j][k] = 1;
}
}
getchar();
}
}
string str, words[maxn];
int count = 0;
getline(cin, str);
string temp = "";
for(int i = 0; i < str.size(); i++){
if(str[i] >= 'A' and str[i] <= 'Z'){
temp += str[i];
if(i == str.size() - 1){
words[count++] = temp;
}
}else{
if(temp.size() != 0){
words[count++] = temp;
}
temp = "";
}
}
for(int i = 0; i < count; i++){
string nowstr = words[i];
for(int j = 0; j < 7; j++){
for(int l = 0; l < nowstr.size(); l++){
for(int k = 0; k < 5; k++){
int x = nowstr[l] - 'A';
if(Letter[x][j][k] == 1){
printf("%c", 'C');
}else{
printf("%c", '.');
}
if(k == 4 and l < nowstr.size() - 1){
printf(" ");
}
}
}
printf("\n");
}
if(i < count - 1){
printf("\n");
}
}
}
本文介绍了一种将输入的字符串转换为特定字符图案的算法实现。重点讲解了如何通过逐行读取和处理输入字符串来生成对应的大写字母图案。讨论了读取字符串时需要注意的细节,例如处理换行符、使用getline读取包含空格的字符串以及确保最后一个单词能够被正确读取的方法。
1173

被折叠的 条评论
为什么被折叠?



