C语言中基于用户输入的数组打印
我正在尝试编写一个程序,其中用户输入文本,控制台以7×5的星号网格打印文本 e、 g 但是,我在识别字符并让程序知道时遇到了困难 我迄今为止的努力:C语言中基于用户输入的数组打印,c,arrays,C,Arrays,我正在尝试编写一个程序,其中用户输入文本,控制台以7×5的星号网格打印文本 e、 g 但是,我在识别字符并让程序知道时遇到了困难 我迄今为止的努力: #include <stdio.h> #include <string.h> char a[7][6] = { " ", " ", "**** ", " *", "*****", "* *", "*****" }; ...other letters... char word[20]; i
#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
#包括
#包括
字符a[7][6]={
" ",
" ",
"**** ",
" *",
"*****",
"* *",
"*****"
};
……其他信件。。。
字符字[20];
int i;
int j;
字符字母[2];
int main(){//main
fgets(word、sizeof(word)、stdin);
对于(j=0;j
字母以单独的阵列制作,并通过回路逐行打印,以便它们在水平方向上一个接一个地打印
我最好的想法是使用变量
字母
,它会将值更改为从word
读取的当前字符,但我知道它不正确。我将所有字母放入一个数组:
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]);
}
}
int-letterIdx;
对于(j=0;j
这是一个以星号形式打印整个单词的工作示例(代码注释):
你想只使用C还是使用C++特征?BTW,我认为这是一个很好的方法,将整个字母映射到星号网格中,然后将输入解析为一个函数,该函数将字符串/字符转换成网格(多维数组)中转换的星号。你可以用C水平打印。你介意扩展一下你的意思吗?我想把所有的字母都放在一个3d数组中,但我不知道最终如何逐行调用它们。你介意进一步解释一下吗?特别是第三行和第四行,以及程序如何使用输入来查找正确的co数组中字母的组合。
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]);
}
}
#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;
}
*** *** ** ***
* * * * * *
* * * * * *
*** *** * * ***
* * * * *
* **** ** ****