목차
5.1 1차원 배열의 선언
5.2 문자열의 사용
5.3 다차원 배열
5.4 배열의 초기화
5.5 문자열 배열
5.2 문자열의 사용
5.3 다차원 배열
5.4 배열의 초기화
5.5 문자열 배열
본문내용
6, 32, 64, 128 };
/* int pwr[8] = { ... }; 와 같은 효과 */
char prompt[] = "Enter your name: ";
** 2차원 배열의 경우, 열의 수는 반드시 주어져야 한다.
int sqr[][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
** 배열의 초기값은 프로그램의 실행도중에 변경될 수 있다.
예제. CPU와 평균 속도 대응
#include "stdio.h"
long cpu[5][2] = {
8088, 4,
8086, 4,
80286, 10,
80386, 20,
80486, 40
};
main()
{
long processor;
int i;
printf("Enter the number of the processor: ");
scanf("%ld", &processor); /* long int */
for (i=0; i<5; i++)
if (processor == cpu[i][0]); {
printf("Average speed is %d MHz.\n", cpu[i][1]);
break;
}
if (i==5)
printf("Processor not found.\n");
}
배열에 초기값이 주어져도 그 내용은 바뀔 수 있다.
예제. #include "stdio.h"
#include "string.h"
main()
{
char str[80] = "I like C";
strcpy(str, "hello");
printf("%s\n", str);
}
5.5 문자열 배열
문자열 배열은 문자열들을 배열에 저장하는 것으로 2차원 배열을 사용한다.
예. char names[10][40]; 길이가 최대 40인 문자열들을 10개 갖는 문자열 배열
gets(names[2]); names 배열의 세번째 문자열
printf("%s\n", names[0]); 첫번째 문자열을 출력
예. #include "stdio.h"
main()
{
char text[10][80];
int i;
for (i=0; i<10; i++) {
printf("%d: ", i+1);
gets(text[i]);
}
do {
printf("Enter number of string (1-10): ");
scanf("%d", &i);
i--; /* i의 값을 배열의 색인으로 사용하기 위해 조정한다. */
if (i>=0 && i<=9)
printf("%s\n", text[i]);
} while(i>=0);
}
예제. 영어에서 독어로 번역
#include "stdio.h"
#include "string.h"
char words[][2][40] = {
"dog", "Hund",
"no", "nein",
"year", "Jahr",
"child", "Kind",
"I", "Ich",
"drive", "fahren",
"house", "Haus",
"to", "zu",
"", ""
};
main()
{
char english[80];
int i;
printf("Enter English word: ");
gets(english);
i = 0;
while (strcmp(words[i][0], "")) {
if (!strcmp(english, words[i][0])) {
printf("German translation: %s\n", words[i][1]);
break;
}
i++;
} /* while */
if (!strcmp(words[i][0], ""))
printf("Not in dictionary.\n");
}
프로그래밍 연습
5.1절 연습문제 2
5.3절 연습문제 1
5.4절 연습문제 3
5.5절 연습문제 1
종합문제 1-3
종합문제 2-2, 3, 5
/* int pwr[8] = { ... }; 와 같은 효과 */
char prompt[] = "Enter your name: ";
** 2차원 배열의 경우, 열의 수는 반드시 주어져야 한다.
int sqr[][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
** 배열의 초기값은 프로그램의 실행도중에 변경될 수 있다.
예제. CPU와 평균 속도 대응
#include "stdio.h"
long cpu[5][2] = {
8088, 4,
8086, 4,
80286, 10,
80386, 20,
80486, 40
};
main()
{
long processor;
int i;
printf("Enter the number of the processor: ");
scanf("%ld", &processor); /* long int */
for (i=0; i<5; i++)
if (processor == cpu[i][0]); {
printf("Average speed is %d MHz.\n", cpu[i][1]);
break;
}
if (i==5)
printf("Processor not found.\n");
}
배열에 초기값이 주어져도 그 내용은 바뀔 수 있다.
예제. #include "stdio.h"
#include "string.h"
main()
{
char str[80] = "I like C";
strcpy(str, "hello");
printf("%s\n", str);
}
5.5 문자열 배열
문자열 배열은 문자열들을 배열에 저장하는 것으로 2차원 배열을 사용한다.
예. char names[10][40]; 길이가 최대 40인 문자열들을 10개 갖는 문자열 배열
gets(names[2]); names 배열의 세번째 문자열
printf("%s\n", names[0]); 첫번째 문자열을 출력
예. #include "stdio.h"
main()
{
char text[10][80];
int i;
for (i=0; i<10; i++) {
printf("%d: ", i+1);
gets(text[i]);
}
do {
printf("Enter number of string (1-10): ");
scanf("%d", &i);
i--; /* i의 값을 배열의 색인으로 사용하기 위해 조정한다. */
if (i>=0 && i<=9)
printf("%s\n", text[i]);
} while(i>=0);
}
예제. 영어에서 독어로 번역
#include "stdio.h"
#include "string.h"
char words[][2][40] = {
"dog", "Hund",
"no", "nein",
"year", "Jahr",
"child", "Kind",
"I", "Ich",
"drive", "fahren",
"house", "Haus",
"to", "zu",
"", ""
};
main()
{
char english[80];
int i;
printf("Enter English word: ");
gets(english);
i = 0;
while (strcmp(words[i][0], "")) {
if (!strcmp(english, words[i][0])) {
printf("German translation: %s\n", words[i][1]);
break;
}
i++;
} /* while */
if (!strcmp(words[i][0], ""))
printf("Not in dictionary.\n");
}
프로그래밍 연습
5.1절 연습문제 2
5.3절 연습문제 1
5.4절 연습문제 3
5.5절 연습문제 1
종합문제 1-3
종합문제 2-2, 3, 5
추천자료
- Visual C++6.0으로 배우는 C언어 제5부-배열변수와 연산자(5/7)
- 배열을 이용 문자열을 거꾸로 출력하기
- C언어 입출력(문자입력 배열을 이용한 문자열 입력, 포인터를 이용한 문자열입력)
- [C언어]배열을 이용한 이진탐색트리 삽입, 삭제, 탐색 연산 프로그램
- C프로그래밍-배열과 포인터에 관해서
- C언어 문자열 자르기(for이용, strtok이용)
- 파일로 부터 문자열을 입력받아 대문자를 소문자로 소문자를 대문자로 바꿔서 저장하는 프로그램
- 키보드로부터 입력(표준입력)받은 한 줄의 문자열을 역(문자의 순서를 반대로)으로 출력하는 ...
- 파일 입출력 및 문자열 관련