# 1.5.3 실습 문제 3가지

<p class="callout info">**실습 전 이론**</p>

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/GzC2TFJaFQ2A6GKL-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/GzC2TFJaFQ2A6GKL-image.png)

**책에서 실습 문제를 내줬다.**

**이전에 만들어 놨던 프로그램은 EOF 신호를 받으면 그동안의 문자들을 계산하기 때문에 거기서 조금 변형을 하면 될것 같다.**

<p class="callout info">**실습**</p>

##### **1. 첫번쨰 문제 - 빈칸, tab, 행의 갯수를 세는 프로그램**

```c
#include <stdio.h>

int main(){
    int c;
    int blankCount = 0;
    int tabCount = 0;
    int lnCount = 0;


    while ((c = getchar()) != EOF){
        if(c == '\n')
            ++lnCount;
        if(c == ' ')
            ++blankCount;
        if(c == '\t')
            ++tabCount;
    }
    printf("개행의 갯수는 %d 개 입니다.\n빈칸의 갯수는 %d 개 입니다.\n탭의 갯수는 %d 개 입니다.\n", lnCount, blankCount, tabCount);
    return 0;
}
```

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/JZBL9Zq5syBRX2R7-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/JZBL9Zq5syBRX2R7-image.png)

**기능상 문제는 없는듯**

**\*물론 불필요하게 긴 코드가 존재할 수는 있겠다.**

##### **2. 두번째 문제 - 한 파일을 읽고 그 중 빈칸이 연달아 나오면 그것을 모두 한칸으로 만들어 출력하는 프로그램을 작성**

**음 if 문으로 공백을 찾고 그 다음 것도 공백일 경우 삭제를 하는 방법으로 해볼까**

```c
#include <stdio.h>
int main(){
    int c;
    int firstBlank;

    firstBlank = 0;
    while ((c = getchar()) != EOF){

        if (firstBlank == 0){
            // printf("[!] firstBlank가 0 이므로 putchar가 실행됩니다.\n");
            putchar(c);
        }
        if (firstBlank == 1 && c != ' '){
            firstBlank = 0;
            // printf("[!] 띄어쓰기 미 탐지, firstBlank가 0(으)로 변환됩니다.\n");            
        }
        if (c == ' '){
            firstBlank = 1;
            // printf("[!] 띄어쓰기 탐지, firstBlank가 1(으)로 변환됩니다.\n");
        }
    }
    

    return 0;
}
```

**생각보다 애먹었다; 무조건 getchar로 한글자씩만 가져오다보니까; 따로 firstBlank 라는 변수를 지정해서 연속으로 오는 공백인지를 체크했다.**

**이거 책에 답이 안적혀있어서 뭐 어떤식으로 했어야됐는지는 모르겠다.**

#### **3. 세번째 문제 - tab을 \\t 로 backspace를 \\b로 \\ 를 \\\\ 로 대치하여 출력하는 프로그램**

**와 이건 너무 모르겠다 보류**

<p class="callout info">**실습 후 이론**</p>