강의

멘토링

커뮤니티

Inflearn Community Q&A

gs12371518's profile image
gs12371518

asked

Learning C language through problems

A into #

41. A를 #으로에서 질문 있습니다.

Written on

·

278

·

Edited

0

풀이를 보기 전에는 string.h의 존재를 몰라서 아래처럼 작성했었습니다.

#include<stdio.h>
int main() {
	int i;
	char word[100];
	scanf("%s", word);
	for (i = 0; i < 100; i++) {
		if (word[i] == 'A') word[i] = '#';
		if (word[i] == NULL) break;
	}
	printf("%s", word);

여기서 궁금한 것은 for문 속 두번째 줄의 의도가 for문에서 쓸데없이 계속 돌지 않게 하기 위한 것인데, 맞게 반영 된건지 궁금합니다.

그러니까 word를 APPLE로 입력했을 때 for문 속에서 i = 5일 때 break가 제대로 작동 되는지 궁금합니다!

c

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네. 잘 break 됩니다. break가 잘 작동하는지 알고 싶으시면 printf를 해보면 됩니다.

#include<stdio.h>
int main() {
	int i;
	char word[100];
	scanf("%s", word);
	for (i = 0; i < 100; i++) {
		if (word[i] == 'A') word[i] = '#';
		if (word[i] == NULL) break;
		printf("%d\n", i);
	}
	printf("%s", word);
}
gs12371518's profile image
gs12371518

asked

Ask a question