본문 바로가기
{Programing}/C.C++

c/c++ 문자열

by 탱타로케이 2020. 1. 14.

문자열 : 문자 배열이라는 의미로  여러 문자를 연속해서 저장한 배열을 뜻한다.

 

char 포인터에 작성하거나 

std::string 클래스를 이용하여 작성한다.

 

 

c style

char * str1 = "Hello";

char str2[] = "world";


char * str3 = "abcde";

printf("%c\n", *(str3+3));
printf("%c\n", str3[2]);

문자열 끝에 항상 null 문자가 붙는다. (\0)

배열처럼 인덱스 접근이 가능하다.

포인터 연산으로도 접근 가능하다.

 

문자열을 자르고 붙이고 비교하는 함수들은 

string.h 파일에 구현되어 있다.

 

 

C Library - - Tutorialspoint

C Library - Advertisements The string.h header defines one variable type, one macro, and various functions for manipulating arrays of characters. Library Variables Following is the variable type defined in the header string.h − Sr.No. Variable & Descriptio

www.tutorialspoint.com

 

c++ style

#include <string>

std::string str("Hello");

std::string str2;

str2 = "world";

std::string str3(str2);

클래스 기반의 문자열로

연산자 오버로딩 및 다양한 함수들이 구현되어있다.

string 헤더 파일에 구현되어있다.

 

 

C++ Strings - Tutorialspoint

C++ Strings Advertisements C++ provides following two types of string representations − The C-style character string. The string class type introduced with Standard C++. The C-Style Character String The C-style character string originated within the C lang

www.tutorialspoint.com

 

'{Programing} > C.C++' 카테고리의 다른 글

c/c++ 상속(1)  (0) 2020.01.21
c/c++ 파일 입출력  (0) 2020.01.17
c/c++ 템플릿  (0) 2020.01.13
c/c++ enum, preprocesser  (0) 2020.01.06
c/c++ const 예약어  (0) 2020.01.06

댓글