일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- charactercreator
- C
- autodesk
- animating
- Costume
- 개발
- 개발블로그
- ue5
- cc4
- visualstudio
- modeling
- 언리얼엔진
- c++
- 마야
- 애니메이팅
- iclone
- ANIMATION
- js
- 티스토리챌린지
- C언어
- HTML
- 3d
- JavaScript
- 프로그래밍
- unreal
- UnrealEngine
- 오블완
- 3dmodeling
- reallusion
- character
- Today
- Total
개발나라 스용공주
[C++] #003 문자열 예제 출력해보기 Simple String Example of C++ with Visual Studio 2022 본문
[C++] #003 문자열 예제 출력해보기 Simple String Example of C++ with Visual Studio 2022
스용공주 2023. 2. 17. 01:19오늘은 비주얼스튜디오를 통해 문자열 예제를 출력하는 프로그램을 짜보겠습니다.
기본 파일 생성은 아래 첫번째 링크를, 프로젝트 속성 설정은 두번째 링크를 참고하여 먼저 만들어주신 뒤 아래 코드만 해당 파일명을 가진 파일에 기존 내용을 삭제하고 넣어주시면 됩니다.
https://hwangseoyoung.tistory.com/22
[C++] #002 비주얼스튜디오(Visual Studio 2022)로 간단한 입출력해보기 Simple InputOutput Example with Visual Stud
오늘은 비주얼스튜디오를 통해 간단한 입출력을 하는 프로그램을 짜보겠습니다. 우선 저는 비주얼스튜디오2022(Visual Studio 2022)을 사용하였습니다. [비주얼스튜디오 2022 다운받기 링크] https://visua
hwangseoyoung.tistory.com
https://hwangseoyoung.tistory.com/21
[C++] #001 비주얼스튜디오 프로젝트 속성 설정하기 Set Project properties with Visual Studio 2022
오늘은 앞으로 사용할 비주얼스튜디오에서의 프로젝트 속성 값을 변경해주는 작업을 담은 글이다. 이 속성 값을 변경하면 따로 번거롭게 경로를 설정하지 않고도 헤더파일만 명시해주면 프로
hwangseoyoung.tistory.com
[01_Test.h]
#pragma once
void String();
헤더파일에 String()의 함수 원형을 선언해줍니다.
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
cout << "Hello World!" << endl;
string str = "Happy a nice day!";
cout << "str 문자열의 길이 : " << str.length() << endl;
cout << "str[0]의 문자 : " << str[0] << endl;
}
}
[결과] |
Hello World! |
str 문자열의 길이 : 17 |
str[0]의 문자 : H |
- length() : 문자열의 길이
- 문자열의 배열 : 해당 문자열의 인덱스 값을 출력
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
string Tuesday = "화요일";
cout << Tuesday << endl;
}
}
[결과] |
화요일 |
- String형 변수 선언과 동시에 초기화하여 출력해준 결과입니다.
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
string banana = "바나나";
string shake = "쉐이크";
string bananashake = banana + shake;
cout << bananashake << endl;
string choco = "초코";
choco += shake;
cout << choco << endl;
}
}
[결과] |
바나나쉐이크 |
초코쉐이크 |
문자열의 결합 예제로, 결합 연산과 추가 연산 2가지 형태가 있습니다.
- 결합 연산 : 선언된 2개의 변수 각각을 더해 새로운 변수에 저장해주는 형태
- 추가 연산 : 기존에 선언된 변수에 새로운 변수를 더해서 저장해주는 형태
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
string str1;
string str2 = "cookie";
string str3 = "내가 만든 쿠키";
cout << "문자열 str1의 길이 : " << str1.length() << endl;
cout << "문자열 str2의 크기 : " << str2.size() << endl;
cout << "문자열 str3의 크기 : " << str3.size() << endl;
}
}
[결과] |
문자열 str1의 길이 : 0 |
문자열 str2의 크기 : 6 |
문자열 str3의 크기 : 14 |
- length() : 문자열의 길이
- size() : string 객체가 메모리에서 실제 사용하고 있는 크기
=> 아무것도 입력하지 않은 문자열의 길이는 0값을 출력하는 것을 알 수 있다.
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
string str4;
cout << str4.append("C++") << endl;
string str5;
cout << str5.append("I made a cookie!", 2, 4) << endl;
string str6;
cout << str6.append(10, 'a') << endl;
}
}
[결과] |
C++ |
made |
aaaaaaaaaa |
- append()
1) 추가할 문자열을 맨 끝에 추가
2) 추가할 문자열의 시작 위치부터 개수만큼 맨 끝에 추가
3) 추가할 문자를 개수만큼 맨 끝에 추가
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
string text = "I will wake up early!";
cout << "wake의 인덱스 값 : " << text.find("wake") << endl;
cout << "u의 인덱스 값 : " << text.find('u') << endl;
if (text.find("Hi") != string::npos)
cout << "Find!" << endl;
else
cout << "Not Find!" << endl;
}
}
[결과] |
wake의 인덱스 값 : 7 |
u의 인덱스 값 : 12 |
Not Find! |
- find()
1) 인덱스 0부터 문자열을 찾아, 그 시작 위치를 반환
2) 인덱스 0부터 찾을 문자를 찾아, 그 시작 위치를 반환
(문자열을 찾지 못하면 string::size_type의 string::npos라는 상수를 반환)
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
string str7 = "apple";
string str8 = "cookie";
if (str7.compare(str8) == 0)
cout << "str7과 str8은 같습니다!" << endl;
else if (str7.compare(str8) < 0)
cout << "str7이 str8보다 사전 편찬 순으로 앞" << endl;
else
cout << "str7이 str8보다 사전 편찬 순으로 뒤" << endl;
}
}
[결과] |
str7이 str8보다 사전 편찬 순으로 앞 |
- compare() : 두 문자열 간의 내용을 비교하는 메서드
[01_Test.cpp]
#include "Stdafx.h"
#include "01_Test.h"
void String()
{
{
string sentence1 = "I made a choco pie yesterday!";
string word1 = "choco";
string word2 = "apple";
string::size_type sentence2 = sentence1.find(word1);
if (sentence2 != string::npos)
sentence1.replace(sentence2, word1.length(), word2);
cout << sentence1 << endl;
}
}
[결과] |
I made a apple pie yesterday! |
- replace() : 특정 문자열을 찾아서 다른 문자열로 대체하는 메소드