Значение счетчика меняется само по себе - C (СИ)

Узнай цену своей работы

Формулировка задачи:

/***********************************************************
 *  A01_Part01 -- program to magnify the text input by                *
 *      drawing all characters - 5x7 - using "*"                         *
 *      character.                                                    *
 *                                                                *
 *  Author: Alexander Zhizhkun.                                          *
 *                                                               *
 *  Purpose: Demonstration of string and I/O handling.                *
 *                                                                *
 *  Usage: Type in the string and get output magnified.                *
 ***********************************************************/
 
#include <stdio.h>
#include <string.h>
int magnified_string_print(char fline[100])
{
    char magnified_line[6][599];    /*
                     * multidimensional array of chars to contain magnified characters,
                     * because characters are 5x7, we have seven 600-character strings
                     * 5*100 (to make appropriate width for 100 chars) + 99 (spaces between 100 chars) + 1 (\0 char) = 600 chars
                     */
    int i;      /* index into the string */
 
    for (i = 0; fline[i] != '\0'; ++i)
    {
        switch (fline[i]){
            case 'A':
            case 'a':
                strcat(magnified_line[0],"  *  ");
                strcat(magnified_line[1]," * * ");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"*****");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6],"*   *");
                break;
            case 'B':
            case 'b':
                strcat(magnified_line[0],"**** ");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"**** ");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6],"**** ");
                break;
            case 'C':
            case 'c':
                strcat(magnified_line[0],"  ***");
                strcat(magnified_line[1]," *   ");
                strcat(magnified_line[2],"*    ");
                strcat(magnified_line[3],"*    ");
                strcat(magnified_line[4],"*    ");
                strcat(magnified_line[5]," *   ");
                strcat(magnified_line[6],"  ***");
                break;
            case 'D':
            case 'd':
                strcat(magnified_line[0],"***  ");
                strcat(magnified_line[1],"*  * ");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5],"*  * ");
                strcat(magnified_line[6],"***  ");
                break;
            case 'E':
            case 'e':
                strcat(magnified_line[0],"*****");
                strcat(magnified_line[1],"*    ");
                strcat(magnified_line[2],"*    ");
                strcat(magnified_line[3],"*****");
                strcat(magnified_line[4],"*    ");
                strcat(magnified_line[5],"*    ");
                strcat(magnified_line[6],"*****");
                break;
            case 'F':
            case 'f':
                strcat(magnified_line[0],"*****");
                strcat(magnified_line[1],"*    ");
                strcat(magnified_line[2],"*    ");
                strcat(magnified_line[3],"*****");
                strcat(magnified_line[4],"*    ");
                strcat(magnified_line[5],"*    ");
                strcat(magnified_line[6],"*    ");
                break;
            case 'G':
            case 'g':
                strcat(magnified_line[0]," ****");
                strcat(magnified_line[1],"*    ");
                strcat(magnified_line[2],"*    ");
                strcat(magnified_line[3],"* ***");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6]," ****");
                break;
            case 'H':
            case 'h':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*****");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6],"*   *");
                break;
            case 'I':
            case 'i':
                strcat(magnified_line[0],"*****");
                strcat(magnified_line[1],"  *  ");
                strcat(magnified_line[2],"  *  ");
                strcat(magnified_line[3],"  *  ");
                strcat(magnified_line[4],"  *  ");
                strcat(magnified_line[5],"  *  ");
                strcat(magnified_line[6],"*****");
                break;
            case 'J':
            case 'j':
                strcat(magnified_line[0],"*****");
                strcat(magnified_line[1],"    *");
                strcat(magnified_line[2],"    *");
                strcat(magnified_line[3],"    *");
                strcat(magnified_line[4],"    *");
                strcat(magnified_line[5],"    *");
                strcat(magnified_line[6],"**** ");
                break;
            case 'K':
            case 'k':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*  * ");
                strcat(magnified_line[2],"* *  ");
                strcat(magnified_line[3],"**   ");
                strcat(magnified_line[4],"* *  ");
                strcat(magnified_line[5],"*  * ");
                strcat(magnified_line[6],"*   *");
                break;
            case 'L':
            case 'l':
                strcat(magnified_line[0],"*    ");
                strcat(magnified_line[1],"*    ");
                strcat(magnified_line[2],"*    ");
                strcat(magnified_line[3],"*    ");
                strcat(magnified_line[4],"*    ");
                strcat(magnified_line[5],"*    ");
                strcat(magnified_line[6],"*****");
                break;
            case 'M':
            case 'm':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"** **");
                strcat(magnified_line[2],"* * *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6],"*   *");
                break;
            case 'N':
            case 'n':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"**  *");
                strcat(magnified_line[3],"* * *");
                strcat(magnified_line[4],"*  **");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6],"*   *");
                break;
            case 'O':
            case 'o':
                strcat(magnified_line[0]," *** ");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6]," *** ");
                break;
            case 'P':
            case 'p':
                strcat(magnified_line[0],"**** ");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"**** ");
                strcat(magnified_line[4],"*    ");
                strcat(magnified_line[5],"*    ");
                strcat(magnified_line[6],"*    ");
                break;
            case 'Q':
            case 'q':
                strcat(magnified_line[0]," *** ");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"* * *");
                strcat(magnified_line[5],"*  * ");
                strcat(magnified_line[6]," ** *");
                break;
            case 'R':
            case 'r':
                strcat(magnified_line[0],"**** ");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"**** ");
                strcat(magnified_line[4],"* *  ");
                strcat(magnified_line[5],"*  * ");
                strcat(magnified_line[6],"*   *");
                break;
            case 'S':
            case 's':
                strcat(magnified_line[0]," *** ");
                strcat(magnified_line[1],"*    ");
                strcat(magnified_line[2]," *   ");
                strcat(magnified_line[3],"  *  ");
                strcat(magnified_line[4],"   * ");
                strcat(magnified_line[5],"    *");
                strcat(magnified_line[6]," *** ");
                break;
            case 'T':
            case 't':
                strcat(magnified_line[0],"*****");
                strcat(magnified_line[1],"  *  ");
                strcat(magnified_line[2],"  *  ");
                strcat(magnified_line[3],"  *  ");
                strcat(magnified_line[4],"  *  ");
                strcat(magnified_line[5],"  *  ");
                strcat(magnified_line[6],"  *  ");
                break;
            case 'U':
            case 'u':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6]," *** ");
                break;
            case 'V':
            case 'v':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"*   *");
                strcat(magnified_line[5]," * * ");
                strcat(magnified_line[6],"  *  ");
                break;
            case 'W':
            case 'w':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2],"*   *");
                strcat(magnified_line[3],"*   *");
                strcat(magnified_line[4],"* * *");
                strcat(magnified_line[5],"** **");
                strcat(magnified_line[6],"*   *");
                break;
            case 'X':
            case 'x':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2]," * * ");
                strcat(magnified_line[3],"  *  ");
                strcat(magnified_line[4]," * * ");
                strcat(magnified_line[5],"*   *");
                strcat(magnified_line[6],"*   *");
                break;
            case 'Y':
            case 'y':
                strcat(magnified_line[0],"*   *");
                strcat(magnified_line[1],"*   *");
                strcat(magnified_line[2]," * * ");
                strcat(magnified_line[3],"  *  ");
                strcat(magnified_line[4],"  *  ");
                strcat(magnified_line[5],"  *  ");
                strcat(magnified_line[6],"  *  ");
                break;
            case 'Z':
            case 'z':
                strcat(magnified_line[0],"*****");
                strcat(magnified_line[1],"    *");
                strcat(magnified_line[2],"   * ");
                strcat(magnified_line[3],"  *  ");
                strcat(magnified_line[4]," *   ");
                strcat(magnified_line[5],"*    ");
                strcat(magnified_line[6],"*****");
                break;
            default:
                strcat(magnified_line[0],"  *  ");
                strcat(magnified_line[1]," *** ");
                strcat(magnified_line[2],"* *  ");
                strcat(magnified_line[3]," *** ");
                strcat(magnified_line[4],"  * *");
                strcat(magnified_line[5]," *** ");
                strcat(magnified_line[6],"  *  ");
                break;
        }
 
        if (fline[i+1]!='\0')
        {
            strcat(magnified_line[0]," ");
            strcat(magnified_line[1]," ");
            strcat(magnified_line[2]," ");
            strcat(magnified_line[3]," ");
            strcat(magnified_line[4]," ");
            strcat(magnified_line[5]," ");
            strcat(magnified_line[6]," ");
        }
    }
    printf("%s\n",magnified_line[0]);
    printf("%s\n",magnified_line[1]);
    printf("%s\n",magnified_line[2]);
    printf("%s\n",magnified_line[3]);
    printf("%s\n",magnified_line[4]);
    printf("%s\n",magnified_line[5]);
    printf("%s\n",magnified_line[6]);
    return i-1;         /* we don't need \0 char to be counted */
}
 
int main()
{
    char line[100];     /* input line */
 
    /* getting input line */
    printf("Would you kindly type something in (to exit just press enter)?");
    fgets(line, sizeof(line), stdin);
 
    magnified_string_print(line);
 
    return 0;
}
Добрый вечер! Программа должна выводить перерисованную звездочками строку. По неизвестной мне причине счетчик i слетает после break в одном из кейсов и берет какое-то неведомое значение, отчего и все проблемы. Что же я делаю не так?

Решение задачи: «Значение счетчика меняется само по себе»

textual
Листинг программы
char magnified_line[6][599] = { 0 };

Объяснение кода листинга программы

  1. Объявляется массив типа char с именем magnified_line и размером 6x599.
  2. Инициализируются все элементы массива значением 0.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.111 из 5
Похожие ответы