문자와 행열을 입력받고 입력받은 문자열을 입력받은행열만큼 출력하기

신동원·2021년 9월 19일
0

C

목록 보기
3/10
#include<stdio.h>

void display(char cr, int lines, int width);

int main()
{
char c;
int rows, cols;

while (1) {
	scanf("%c %d %d", &c, &rows, &cols);
	while (getchar() != '\n') continue;
	display(c, rows, cols);

	if (c == '\n')
		break;
}
return 0;
}

void display(char cr, int lines, int width) {
int a = 0, b = 0;
while (a < lines) {
	while (b < width) {
		printf("%c", cr);
		b++;
	}
	printf("\n");
	a++;
	b = 0;
}
}

while (1) {
scanf("%c %d %d", &c, &rows, &cols);
while (getchar() != '\n') continue;
display(c, rows, cols);

if (c == '\n')
break;
}
문자와 행,열을 순서대로 입력받고
입력받은 문자이후에 공백이 있더라도 전부 읽고 버퍼에서 없애준다.
display함수를 호출하고,
c값이 만약 줄바꿈이라면 무한루프를 탈출한다.


void display(char cr, int lines, int width) {
int a = 0, b = 0;
while (a < lines) {
while (b < width) {
printf("%c", cr);
b++;
}
printf("\n");
a++;
b = 0;
}
}
입력 받은 문자와 행,열만큼 문자를 출력해주는 함수이다.



결과값 출력
a가 2개씩 5줄로 출력되었다.

profile
오늘보다 내일 더 나은 사람이 되기 위해 노력하자

0개의 댓글