쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 12-2
5.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char c;
while(1) {
printf("문자를 입력하시오: ");
c=getchar();
if( islower(c) )
putchar(toupper(c));
if( isupper(c) )
putchar(tolower(c));
if( !isalpha(c) )
printf("경고 ");
if( c=='.') break;
fflush(stdin);
// 줄바꿈 문자 제거
}
return 0;
}
6.
#include <string.h>
#include <stdio.h>
#define SIZE 100
void str_upper(char *s) {
int i;
int count=0;
for (i=0;i<strlen(s);i++) {
if( s[i] >='a' && s[i] <= 'z' )
s[i] = s[i]-'a'+'A';
}
}
int main(void) {
char str[SIZE];
printf("문자열을 입력하시오: ");
gets(str);
str_upper(str);
printf("변환된 문자열: %s\n", str);
return 0;
}
7.
#include <stdio.h>
#include <string.h>
int get_response(char *prompt) {
char response[100];
printf(prompt);
scanf("%s", response);
if( strcmp(response, "yes") == 0 ||
strcmp(response, "y") == 0 ||
strcmp(response, "YES") == 0 ||
strcmp(response, "Y") == 0 )
return 1; else return 0;
}
int main(void) {
int result;
result = get_response("게임을 하시겠습니까");
if( result == 1 )
printf("긍적적인 답변);
else
printf("부정적인 답변);
return 0;
}
8.
#include <string.h>
#include <stdio.h>
char s[] = "Man is immortal, because he has a soul";
char seps[] = " ,\t\n";
char *token;
int main( void ) {
int count=0;
token = strtok( s, seps );
while( token != NULL ) {
count++;
token = strtok( NULL, seps );
}
printf("단어의 수는 %d입니다.\n", count);
return 0;
}
댓글
댓글 쓰기