2. 2
The Use of getchar() and putchar()
getchar()
– keyboard에서 문자를 읽는데 사용한다.
– Buffered scanf와 동일 - data의 read를 위해 Enter key의 입
력이 필요하다
– Enter key를 누르면 Enter key까지 포함한 입력되었던 모든
문자들은 stdin buffer로 보내진다.
– getchar() function은 문자들을 한번에 한문자씩 반환한다.
c = getchar(); /* 한 char를 read하여 c에 입력 */
3. 3
The Use of getchar() and putchar()
putchar() - 화면에 문자를 출력하는데 사용한다.
[Ex]
#include <stdio.h>
int main(void)
{
putchar('S');
putchar('K');
putchar('K');
putchar('U');
}
SKKU
putchar(‘ ‘)에 한 char만
사용가능 함.
4. 4
The Use of getchar() and putchar()
예제
[Ex]
char c = getchar() ;
while(c != ‘ ‘ )
{
putchar( c ) ;
c = getchar() ;
}
[Ex]
while( (c=getchar()) != ‘ ’ )
{
putchar( c ) ;
}
5. 5
caps Program
소문자를 대문자로 대문자는 소문자로 변환하는 예제
#include <stdio.h>
int main(void)
{
char c;
while (( c = getchar() ) != ‘n’) {
if( ‘A’ <= c && c <= ‘Z’ )
putchar( c + (‘a’-’A’) ) ;
else if( ‘a’ <= c && c <= ‘z’ )
putchar( c - (‘a’-’A’) ) ;
else
putchar( c ) ;
}
return 0;
}
6. 6
The Macros in ctype.h
ctype.h 헤더파일은 문자 인자를 검사하는 매크로들을 정의한다.
Character macros
Macro Nonzero (true) is returned if
isalpha(c)
isupper(c)
islower(c)
isdigit(c)
isalnum(c)
isxdigit(c)
isspace(c)
ispunct(c)
isprint(c)
isgraph(c)
iscntrl(c)
isascii(c)
c is a letter
c is an uppercase letter
c is a lowercase letter
c is a digit
c is a letter or digit
c is a hexadecimal digit
c is a white space character
c is a punctuation character
c is a printable charcter
c is a printable, but not a space
c is a control character
c is an ASCII code
7. 7
The Macros in ctype.h
toupper() and tolower() functions
[Ex]
int tolower(int c); /* c가 대문자라면 소문자가 리턴*/
int toupper(int c); /* c가 소문자라면 대문자가 리턴 */
[Ex]
#define _tolower(c) ( (c) + ‘a’ – ‘A’ ) /* 함수를 define하여 지정 */
Character macros and functions
Function or macro Effect
toupper(c)
tolower(c)
toascii(c)
Changes c from lowercase to uppercase
Changes c from uppercase to lowercase
Changes c to ASCII code