打印出数组根据C的用户输入 [英] Printing out arrays based on user input in C

查看:151
本文介绍了打印出数组根据C的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序中,用户输入文本和控制台打印文本在7 5格星号。

I am trying to write a program in which the user inputs text and the console prints that text in a 7 by 5 grid of asterisks.

例如;

Enter a word:
a

**** 
    *
*****
*   *
*****

不过我无法识别的字符,然后让程序知道了。

However I am having trouble identifying the characters and then letting the program know.

我试图至今:

#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

信件在单个阵列制作和印刷经由环行线,以便他们能够打印在另一个水平了。

Letters are made in an individual array and printed line by line via loop in order for them to be printed one after the other horizontally.

我最好的想法是,变量这将改变值到当前字符正在从字读,但我知道这是不正确的。

My best idea was that variable letter which would change value to the current character being read from word but I know it is not correct.

推荐答案

这是一个工作的例子,在星号的形式打印整个单词(通过code注释):

This is a working example that prints a whole word in asterisk form (comments through the code):

#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;
}

虽然我用数字代替文字,微小的变化会适合您的需求。对于这个code,号[] =92 02将打印为:

  ***  ***          **   ***   
 *  *     *        *  *     *  
 *  *     *        *  *     *  
  ***  ***         *  *  ***   
    *  *           *  *  *     
    *  ****         **   ****

这篇关于打印出数组根据C的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆