1. 문자열 복사
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void main() | |
{ | |
char str1[30] = "String function"; | |
char str2[30]; | |
char str3[10]; | |
strcpy(str2, str1); // str1의 문자열을 str2에 복사 | |
puts(str2); | |
strncpy(str3, str1, sizeof(str3)-1); // str1의 문자열을 지정된 크기만큼 str3에 복사 | |
str3[sizeof(str3)-1] = 0; // 마지막 열에 null을 삽입 | |
puts(str3); | |
} |
문자열의 출력함수는 null문자가 나타날때까지 출력하는데 null문자를 넣을 공간을 만들어주지 않으면 출력함수는 null문자를 찾아 엉뚱한 영역의 값까지 출력해버립니다. 그러므로 마지막 열의 null값을 넣어 주어야 합니다.
2. 문자열 덧붙임
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void main() | |
{ | |
char str1[30] = "String"; | |
char str2[30] = "function"; | |
char str3[30] = "4 number : "; | |
char str4[30] = "1234567890"; | |
strcat(str1, str2); // str1 뒷부분에 str2를 덧붙임 | |
puts(str1); | |
strncat(str3, str4, 4); // str3 뒷부분에 str4를 4번째 문자까지만 덧붙임 | |
puts(str3); | |
} |
strncat()함수는 덧붙일 문자열에서 원하는 수만큼만 덧붙게 해줍니다.
3. 문자열 비교
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void main() | |
{ | |
char str1[30] = "String"; | |
char str2[30] = "Stdio"; | |
if(!strcmp(str1, str2)) | |
{ | |
printf("두문자는 완벽일치"); | |
} | |
else if(!strncmp(str1, str2, 1)) // 1만큼만 분석하여 문자열을 비교 | |
{ | |
printf("두문자는 최소 하나 일치"); | |
} | |
else | |
{ | |
printf("두문자는 불일치"); | |
} | |
} |
이 때 문자열의 크고 작음을 비교하는 기준은 아스키코드값을 토대로 일어나게 됩니다. 예를 들어 문자열 "ABC"와 "ACD"를 비교할때 먼저 A는 서로 같기 때문에 다음 문자로 넘어갑니다. B와 C를 비교할때는 아스키 코드값이 C가 더 크므로 strcmp 함수는 0보다 큰 값을 반환하게 됩니다.
4. 문자열 길이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void main() | |
{ | |
char str[30] = "String"; | |
int length; | |
length = strlen(str); | |
printf("문자열의 길이 : ", length); | |
} |
위의 예제에서 문자열은 7의 공간을 차지하고 있지만 null값을 제외하여 6의 값이 반환됩니다.
5. 문자열 분리
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void main() | |
{ | |
char str[30] = "String function"; | |
char *pstr; | |
pstr = strtok(str, " "); | |
printf("%s\n%s\n", pstr, str); | |
pstr = strtok(NULL, "c"); | |
printf("%s\n", pstr); | |
} |
출력결과