我正在尝试编写一个程序,其中用户输入文本,控制台以7乘5的星号网格打印该文本。
e.g;
Enter a word:
a
****
*
*****
* *
*****
但是我无法识别角色,然后让程序知道。
到目前为止我的尝试:
#include <stdio.h>
#include <string.h>
char a[7][6] = {
" ",
" ",
"**** ",
" *",
"*****",
"* *",
"*****"
};
...other letters...
char word[20];
int i;
int j;
char letter[2];
int main() {//main
fgets(word, sizeof(word), stdin);
for (j = 0; j < strlen(word); j++) {
strcpy(letter, word[j]);
for (i = 0; i < 7 ; i++) {
printf("%s %d\n", letter[i]);
}
}
printf("Total number of characters processed: %d\n", strlen(word) - 1);
return (0);
}//*main
字母以单个数组制作,并逐行打印,以便水平地一个接一个地打印。
我最好的想法是变量letter
,它会将值更改为从word
读取的当前字符,但我知道它不正确。
答案 0 :(得分:0)
我将所有字母都放在一个数组中:
char letters[26][7][6] = {{
" ",
" ",
"**** ",
" *",
"*****",
"* *",
"*****"
},
{
"* ",
"* ",
"* ",
"* ",
"*****",
"* *",
"*****"
},
...
};
然后你可以得到这样的合适的人:
int letterIdx;
for (j = 0; j < strlen(word); j++) {
// make sure this character is a letter, if not go to the next one
if (!isalpha(word[i]) continue;
// subtract the ASCII value of 'a' from the ASCII value of
// the lower case version of the current letter to get a number between 0 and 25
letterIdx = tolower(word[i]) - 'a';
for (i = 0; i < 7 ; i++) {
printf("%s\n", letters[letterIdx][i]);
}
}
答案 1 :(得分:0)
这是一个工作示例,以星号形式打印整个单词(通过代码注释):
#include <stdio.h>
#include <stdlib.h>
#define maxchar 10 // number of characters in asterisk form
#define maxrows 6 // vertical length
#define maxcols 5 // horizontal length
#define imax 5 // number of chars to be displayed in asterisk form
int main()
{
// testing word with spaces
char number[imax] = "92 02";
// array of numbers
char letter[maxrows][maxchar][maxcols] =
{ // j=0 , j=1 , j=2 , j=3 , j=4 , j=5 , j=6 , j=7 , j=8 , j=9
{ " ** ", "*** ", "*** ", "*** ", "* *", "****", "* ", "****", " ** ", " ***" }, // row=0
{ "* *", " * ", " *", " *", "* *", "* ", "* ", " *", "* *", "* *" }, // row=1
{ "* *", " * ", " *", " ** ", "****", "*** ", "*** ", " *", " ** ", "* *" }, // row=2
{ "* *", " * ", "*** ", " *", " *", " *", "* *", " *", "* *", " ***" }, // row=3
{ "* *", " * ", "* ", " *", " *", " *", "* *", " *", "* *", " *" }, // row=4
{ " ** ", "****", "****", "*** ", " *", "*** ", "*** ", " *", " ** ", " *" } // row=5
};
// "iterators"
int row, col, i;
for(row=0; row < maxrows; ++row) // print from up to down
{
for(i=0; i < imax; ++i) // for each character in array "number"
{
int j = number[i] - '0'; // get position in "letter". For characters, change to j = number[i] - 'a';
if(j<0)
printf(" "); // if number[i] is not a valid character, print a space
else
for(col = 0; col < maxcols; ++col)
printf("%c", letter[row][j][col]); // print each * at given row
printf(" "); // print a small space between characters
}
printf("\n"); // proceed to next row
}
return 0;
}
虽然我使用的是数字而不是字符,但最小的更改可以满足您的需求。对于此代码,number[] = "92 02"
将打印为:
*** *** ** ***
* * * * * *
* * * * * *
*** *** * * ***
* * * * *
* **** ** ****