Skip to content

Instantly share code, notes, and snippets.

@kyagrd
Created October 22, 2018 02:39
Show Gist options
  • Save kyagrd/32e422717f1a9593ceedb09a2c271a13 to your computer and use it in GitHub Desktop.
Save kyagrd/32e422717f1a9593ceedb09a2c271a13 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
/* K&R 프로그램에서 시작해서 변형했다면
#define IN 0
// 대문자로 시작한 상태
#define INUP 2
#define OUT 1
state = OUT; // 초기상태
OUT -> IN // 대문자 아닌 경우 단어 시작
OUT -> INUP // 대문자인 경우 단어 시작
이 두 경우에는 ++wordcount;
IN -> OUT // 단어 끝 할 일 없음
INUP -> OUT // 대문자 시작한 단어 끝
마지막 경우에 현재 입력받은 글자가 islower(c)
이면 ++ulwordcount;
*/
int main(void)
{
int wordcount = 0;
int ulwdcount = 0;
char first; // 단어 첫글자
char b = ' '; // 직전에 입력받은 문자
char c;
bool loopcond = true; // 종료조건
while (loopcond) {
if (EOF==(c = getchar())) {
loopcond = false; // while문 종료하기 위해
c = ' '; // 입력이 EOF대신 ' '인 것처럼
}
// 직전글자 b가 공백문자이고 입력받은 c가 공백문자 아니면
if (isspace(b) && !isspace(c)) { // c가 단어 시작 글자
first = c;
++wordcount;
}
// 직전글자 b가 공백문자 아니고 입력받은 c가 공백문자이면 단어 끝인데
// 그 중에서도 첫글자가 대문자였고 직전글자 b가 소문자인 경우에
if (!isspace(b) && isspace(c) && isupper(first) && islower(b)) {
++ulwdcount;
}
b = c;
}
printf("%d\n", wordcount); // 전체 단어 개수
printf("%d\n", ulwdcount); // 대문자로 시즉해 소문자로 끈난 단어 개수
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment