과제 01. 음료수 가격 계산 코드 작성 깃허브 링크
https://github.com/coderrim/SWING/commit/cd2bfb64206ee0ef3efa5936c7448830c05634c8
Create SWING C++ 01 · coderrim/SWING@cd2bfb6
coderrim committed Oct 21, 2023
github.com
과제 02. 문서화
cstring 은 문자열 함수로 대표적인 것은 아래와 같다.
strlen : 문자열의 길이를 알려준다.
strcpy : 문자열을 복사한다.
strcat : 두 문자열을 붙인다.
strcmp : 두 문자열을 비교한다.
문자열 함수를 사용하려면 string.h 헤더 파일이 필요하다.
C++문자열은 메모리를 동적으로 할당해서 메모리 낭비가 없다고 한다.
null문자는 \0이다.
예시를 보자.
C++ standard string class 의 예이다.
String name="NAME";
#include<iostream>
#include<string>
using namespace std;
int main() {
string name = "Hyorim";
cout << "The name is : " << name << endl;
return 0;
}
char name[ NAME 글자 수 + 1 ] = "NAME";
string value 를 사용해 문자열 값에 액세스할 수 있다.
#include<iostream>
#include<string>
using namespace std;
int main() {
char name[7] = { 'H','y','o','r','i','m','\0' };
cout << "String value is : ";
cout << name << endl;
return 0;
}
strcpy(str1, str2); 는 문자열 복사 기능이다. str1에 str2를 복사한다.
strcat(str1, str2); 는 str1의 끝에 str2를 연결한다.
strlen(str1); 는 문자열의 길이를 반환한다.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char name1[10] = "Hyorim";
char name2[10] = "Swing";
char name3[10];
int len;
strcpy(name3, name1);
cout << "strcpy(name3, name1) : " << name3 << endl;
strcat(name1, name2);
cout << "strcat(name1, name2) : " << name1 << endl;
len = strlen(name1);
cout << "strlen(name1) : " << len << endl;
return 0;
}
strcmp(str1, str2); 는 문자열 비교 기능이다.
두 문자열이 같다면 0을, 다르다면 -1을 반환한다.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char str1[] = "Hyorim";
char str2[] = "Swing";
int result = strcmp(str1, str2);
cout << result;
return 0;
}
string 클래스의 멤버 함수에 대해 알아보자.
append와 insert는 문자열을 결합할 수 있다.
length는 문자 수를 알 수 있다.
clear는 문자열을 비워주는 초기화 함수고 empty는 문자열이 비었나 확인하는 함수이다.
insert 문자열 결합
string str1=" "
string str2=str1.insert(num, "Z") 라고 입력하면 str1의 num 번째 문자 뒤에 Z가 생기는 것을 확인할 수 있다.
인덱스는 0부터 시작한다.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main() {
string str1 = "abcde";
string str2 = str1.insert(2, "Z");
cout << str2 << endl; //abZcde
return 0;
}
append 문자열 결합
str1.append(str2)
str1.append(str2, 문자 수)
str1.append(str2, 위치, 문자 수)
위치를 생략하면 str1 뒤에, 가장 마지막에 문자가 결합된다.
문자수를 생략하면 지정한 모든 문자열이 결합된다.
abcde.append("ABC") -> abcdeABC
abcde.append("ABC", 1) -> abcdeA
abcde.append("ABC", 0, 2) -> abcdeAB
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main() {
string str1 = "abcde";
string str2 = str1.append("ABC");
cout << str2 << endl; //abcdeABC
return 0;
}
length 는 문자 수를 알려준다.
string txt = " ";
cout << txt.length();
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main() {
string txt = "abcdefghijklmnopqrstuvwxyz";
cout << "The length of the txt string is : " << txt.length();
return 0;
}
str.empty() : 객체가 비어있으면 1을, 문자열이 저장되어 있으면 0을 반환한다.
str.clear() : 문자열을 비워주는 초기화 함수다.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main() {
string str = "abcdefghijklmnopqrstuvwxyz";
if (!str.empty())
{
str.clear();
}
return 0;
}
'C++' 카테고리의 다른 글
[SWING] C++ 06 (0) | 2022.06.27 |
---|---|
[SWING] C++ 05 (0) | 2022.05.23 |
[SWING] C++ 04 (0) | 2022.05.14 |
[SWING] C++ 03 (0) | 2022.05.06 |
[SWING] C++ 02 (0) | 2022.05.01 |