Skip to content

Instantly share code, notes, and snippets.

@dev-yong
Created February 15, 2017 14:24
Show Gist options
  • Save dev-yong/ed3d293a1d0361bbb771694a31f4e8a2 to your computer and use it in GitHub Desktop.
Save dev-yong/ed3d293a1d0361bbb771694a31f4e8a2 to your computer and use it in GitHub Desktop.
문자열(교육용)
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
/*
char char_arr[5] = { 'A','P','P','L','E' };
char char_arr[5]="APPLE";
어째서 에러가 날까?
문자열을 초기화할 때, 문자열의 끝에 자동으로 NULL문자('\0')가 입력되어 n개의 글자를 받기 위해서는 n+1개의 공간이 필요로 하다.
char char_arr[6] = "APPLE";
*/
/*
char str[15];
scanf("%s", str);
printf("%s", str);
scanf문에서는 문자열을 입력받을 때 공백문자들로 구분을 한다.
따라서, 문장 "I have a apple pan" 같은 경우 I만 입력 받게 된다.
그 대응으로, gets()함수를 사용한다. (strcpy, gets, strcat 함수 등은 버퍼 오버플로우에 노출되어있다.)
gets_s(str);
%s로 문자열을 출력할 때, 문자열의 끝을 아는 방법은 NULL문자로 판별
*/
/*
char *str = "Time flies so fst.";
str[2] = 'n';// 포인터형 문자열은 수정 불가능
printf("%s", str);
*/
/*char str[12] = "Pan";
char temp[] = "Apple";
printf("%d\n", strlen(str)); //string length
printf("%d\n", strcmp(str, "Astrology")); //string compare
왼쪽 문자열이 더 크면 리턴값은 1,
두 문자열이 같으면 리턴값은 0,
오른쪽 문자열이 더 크면 리턴값은 -1
strcpy_s(str, temp); //string copy
printf("%s\n", str);
strncpy_s(str, temp, 5); //string n copy
printf("%s\n", str);
strcat_s(str, " Pan"); //string concatenation(연결)
printf("%s\n", str);*/
/*
//char word[][10] = {"Polar", "Volcano", "City", "Desert", "Wood"};
char *word[] = { "Polar", "Volcano", "City", "Desert", "Wood" };
//배열의 원소가 포인터. 각 원소는 어딘가의 주소값
for (int i = 0; i < 5; i++) {
printf("%s\n", word[i]);
}
*/
/*
printf("%d", 'A');
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment