쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 12-1

1.

#include <stdio.h>

int main(void) {

char ch;

printf("문자를 입력하시오: ");
scanf("%c", &ch);
printf("아스키 코드값=%d\n", ch);

return 0;
}

2.

#include <string.h>
#include <stdio.h>

#define SIZE 100

void delete_space(char s[]) {

char tmp[SIZE];

int i, k = 0;

for (i = 0; i < (int)strlen(s); i++) {
if (s[i] != ' ')
tmp[k++] = s[i];
}

tmp[k] = 0;

strcpy(s, tmp);
}

int main(void) {

char str[SIZE];

printf("공백 문자가 있는 문자열을 입력하시오: ");

gets(str);

delete_space(str);

printf("%s", str);
return 0;
}

3.

#include <string.h>
#include <stdio.h>

#define SIZE 100

int str_chr(char* s, int c) {

int i;
int count = 0;

for (i = 0; i < strlen(s); i++) {

if (s[i] == c)
count++;
}

return count;
}

int main(void) {

char str[SIZE];
char ch;

printf("문자열을 입력하시오: ");

gets(str);

printf("개수를 셀 문자를 입력하시오: ");

ch = getchar();

printf("%c의 개수: %d", ch, str_chr(str, ch));

return 0;
}

4.

#include <string.h>
#include <stdio.h>

#define SIZE 100

int str_chr(char* s, int c) {

int i;
int count = 0;

for (i = 0; i < strlen(s); i++) {
if (s[i] == c)
count++;
}

return count;
}

int main(void) {

char str[SIZE];
char ch;

printf("문자열을 입력하시오: ");

gets(str);

for (ch = 'a'; ch <= 'z'; ch++) {
printf("%c: %d \n", ch, str_chr(str, ch));
}

return 0;
}

댓글

이 블로그의 인기 게시물

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 10-3

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 16-1

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 13-2