pat basic 1109 Good at C

When you are asked by the interviewer to write "Hello World" in C, do you have the ability to write it as shown in the picture below?

Input format:

The input first gives 26 English uppercase letters AZ, each letter is composed of a 7×5 matrix composed of Cand . .Finally give a sentence on one line, ending with a carriage return. A sentence is composed of several words (each containing no more than 10 consecutive uppercase English letters) separated by any non-capital English letters.

The question is guaranteed to give at least one word.

Output format:

For each word, output each letter in a row in matrix form, with a column of spaces separating the letters. There must be no extra spaces at the beginning or end of the word.

There must be a blank line between two adjacent words. There must be no extra blank lines at the beginning or end of the output.

Input example:

(Too long... skip it), the last line:

HELLO~WORLD!

Output sample:

C...C CCCCC C.... C.... .CCC.
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
CCCCC CCCC. C.... C.... C...C
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
C...C CCCCC CCCCC CCCCC .CCC.

C...C .CCC. CCCC. C.... CCCC.
C...C C...C C...C C.... C...C
C...C C...C CCCC. C.... C...C
C.C.C C...C CC... C.... C...C
CC.CC C...C C.C.. C.... C...C
C...C C...C C..C. C.... C...C
C...C .CCC. C...C CCCCC CCCC.

Problem-solving ideas:

The shape of each large letter can be saved in a two-dimensional array, and a structure array is used to save the shape of the 26 letters. First use a function to read the shape of each large letter. Note that there is a carriage return at the end of each line. You can use getchar() to read it out.

Output can also be processed using functions, passing in the structure array that holds the shape, the string to be output, and the length of the string as parameters. Subtract 'A' from the capital letter to get the subscript of the structure array. First print the first line of each letter, then print the second line of each letter...and so on. Pay attention to the spaces between large letters, and wrap each line after the output is completed. In this way, you can get results that meet the requirements.

Another problem is to deal with the blank line requirement between characters. I use the flag to determine whether the output is the first string. Except for one string, all others have a blank line before output, so that there will be no line at the end. There are extra blank lines.

More test data:

%%^^HELLO

HELLO~~##

HELLOowWORLD

~~--HELLO((^WORLD$$%

If there is no problem with the format, it should be successful.

#include <stdio.h>

typedef struct node *PtrC;
typedef struct node {
	char item[7][5];
} BigChar;

void ReadBigChar(PtrC template) {
	for ( int row=0; row<7; ++row ) {
		for ( int col=0; col<5; ++col ) {
			template->item[row][col] = getchar();
		}
		getchar();
	}
}

void Print(BigChar template[], char *str, int len) {
	int row, col, i;
	for ( row=0; row<7; ++row ) {
		for ( i=0; i<len; ++i ) {
			if ( i != 0 ) putchar(' ');
			for ( col=0; col<5; ++col )
				printf("%c", template[str[i]-'A'].item[row][col]);
		}
		if ( row != 6 ) putchar('\n');
	}
}

int main(int argc, const char *argv[]) {
	int i, flag;
	char c, str[12];
	BigChar template[26];
	for ( i=0; i<26; ++i )
		ReadBigChar(&template[i]);

	flag = i = 0;
	while ( (c = getchar()) != '\n' ) {
		if ( c >= 'A' && c <= 'Z' ) {
			str[i++] = c;
		} else {
			str[i] = 0;
			if ( i > 0 ) {
				flag ? printf("\n\n") : (flag = 1);
				Print(template, str, i);
			}
			i = 0;
		}
	}
	str[i] = 0;
	if ( i > 0 ) {
		flag ? printf("\n\n") : (flag = 1);
		Print(template, str, i);
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/herbertyellow/article/details/127025352