Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 이분 탐색(Binary Search)
- 스택(Stack)
- 플로이드 워셜 알고리즘(Floyd-Warshall Algorithm)
- 그래프(Graph)
- 알고리즘 개념
- 백준 18352번
- 백준 21606번
- 백준 2261번
- 백준 2812번
- DFS(Depth First Search)
- 백준 9012번
- 동적 프로그래밍(Dynamic Programming)
- 위상 정렬(Topology Sort)
- BFS
- BFS(Breadth First Search)
- 그리디 알고리즘(Greedy Algorithm)
- 이분 그래프(Bipartite Graph)
- DFS & BFS
- 백준 17608번
- 다익스트라 알고리즘(Dijkstra Algorithm)
- 백준 1707번
- DFS
- 큐(Queue)
- 백준 1948번
- 분할 정복(Divide and Conquer)
- 백준 2504번
- 백준 2493번
- 백준 10000번
- 트리(Tree)
- 위상 정렬(Topological Sort)
Archives
- Today
- Total
Always Be Wise
문자열 본문
728x90
컴퓨터는 숫자를 글자에 대응시키는 방식으로 문자를 사용한다. 예를 들어, 알파벳 'a'는 숫자 97에 대응된다.
이런 식으로 문자 하나 하나를 숫자에 매칭시키고 표현하는 방식을 아스키 코드(ASCII CODE)라고 한다.
그런데 1바이트를 사용하는 경우, 숫자와 문자를 매칭 시킬 경우 최대 256개의 글자 밖에 표현을 못한다.
이런 제한을 극복하기 위해 유니코드(UNICODE)를 사용한다. 유니코드는 문자를 2바이트로 처리하여 기존의 256배, 65,536개의
글자를 표현할 수 있다.
#include <stdio.h>
int main()
{
char ch[7] = { 'a', 'b', 'c', 'd', 0, 'e', 'f' };
printf("ch 는 %s", ch);
return 0;
}
여러 정수를 하나의 변수에 담고 싶을 때 배열을 사용하듯, 하나의 변수에 여러 글자를 담고 싶다면 char 형 배열을 사용한다.
그런데 주의할 점은 위와 같이 배열을 선언할 때 바로 값을 넣어주어야 한다. 만약, 선언을 한 이후에 값을 대입하고 싶다면
ch[0] = 'a', ch[1] = 'b'와 같이 하나하나 저장해야 한다. 또한, 컴퓨터의 이해 방식에 의해 문자 배열의 마지막 자리에 종료 문자가
(0, \0, NULL 등) 있어야 한다.
char c[] = "c string";
아래 코드와 같이 문자열은 한번 선언되고 나면 다시 재 할당하는 것이 안된다. 이때는 strcpy 함수를 사용해야 한다.
char c[100];
c = "C programming"; // Error! array type is not assignable.
// Use the strcpy() function to copy the string instead.
// char* strcpy(char* destination, const char* source);
// The strcpy() function is defined in the string.h header file.
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20];
// copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
return 0;
}
이외에도 문자열 관련 유용한 함수들이 많다.
Example: strlen( ) function
#include <stdio.h>
#include <string.h>
int main()
{
// strlen() 함수 : 문자열의 길이를 알 수 있다.
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
// using the %zu format specifier to print size_t
printf("Length of string a = %zu \n",strlen(a)); // 7
printf("Length of string b = %zu \n",strlen(b)); // 7
return 0;
}
Example: strcmp( ) function
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result); // 1
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result); // 0
return 0;
}
Example: strcat( ) function
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "c programming";
// concatenates str1 and str2
// the resultant string is stored in str1.
strcat(str1, str2);
puts(str1); // This is c programming
puts(str2); // programming
return 0;
}
Comments