ASCII/ Unicode 차이
char 정리
char string.h
strlen을 for안에 넣으면 안됨
strcpy 대신 memcpy 쓰는것이 더 효율적
string class 소개 (strlen, strcpy, strcat, strstr)
strcmp(str1, str2) == 0 (동일 결과 나옴 !strcmp(str1,str2)) -> 같음
-> size, find, substr
-------------------------------------------0728
*
*
#include<iostream>
using namespace std;
int myswap (int* p, int* q) {
int backup = *p;
*p = *q;
*q = backup;
*p = 7;
return 3;
}
int main() {
int a = 3;
int b = 7;
myswap(&a, &b);
cout << a << b;
return 0;
}
*
*
#include<iostream>
using namespace std;
int ABC (int* p) {
*p = 7;
return 3;
}
int main() {
int a = 3;
int b = ABC(&a);
cout << a << endl;
cout << b;
//int ret;
//int a = ABC(&ret);
//cout << ret << endl; //7
//cout << a; //3
//
return 0;
}
*
*
#include<iostream>
using namespace std;
int main() {
int a; // 선언
a = 3; // 대입연산 저장
// 1. 포인터변수는 주소값을 저장한다.
int* p; // 포인터 변수 선언
p = &a; // 주소값 저장
// 2. 주소값을 저장하면 가리킨다 라고 표현을 합니다.
// 3. 가리키게 되면 원격조종을 할 수 있다.(p를 통해서 a를 조종)
cout << *p << endl; //3
*p = 17;
cout << a; //17
return 0;
}
* /////////////////////////////////////////////////////////////////////
* 구조체 예시2
#include<iostream>
#include<vector>
using namespace std;
struct Node { //type을 만듦, 변수만든것 x
int a;
char b;
};
int main() {
Node arr[6];
for (int i = 0; i < 6; i++) {
arr[i].a = 9-i;
arr[i].b = char('a'+i);
}
for (int i = 0; i < 6; i++) {
cout << arr[i].a << " ";
}
cout << endl;
for (int i = 0; i < 6; i++) {
cout << arr[i].b << " ";
}
return 0;
}
*
* 구조체 예시1
#include<iostream>
using namespace std;
struct ABC { //type을 만듦, 변수만든것 x
int a;
int b;
};
int main() {
ABC t = { 3,7 }; // 선언 + 초기화;
ABC q;
q.a = 3;
q.b = 7;
ABC v;
v = { 3,7 };
return 0;
}
*
*
//유효성검사
#include <iostream>
#include <string>
using namespace std;
string id;
bool isValid()
{
int n = id.size();
//id가 3 ~ 8 글자 까지 허용 (3, 8 포함)
if (!(n >= 3 && n <= 8)) return false;
//첫글자는 대문자로 시작해야함
if (!(id[0] >= 'A' && id[0] <= 'Z')) return false;
//id에 영어 대소문자만 허용
for (int i = 0; i < n; i++) {
if (id[i] >= 'A' && id[i] <= 'Z') continue;
if (id[i] >= 'a' && id[i] <= 'z') continue;
return false;
}
//탈락조건만 넣고,
//여기까지 살아남았으면 합격
return true;
}
int main()
{
//cin >> id;
id = "SDasd";
bool ret = isValid();
cout << ret;
//출력결과 (둘중 하나 출력)
//OK VALID
//NO INVALID
return 0;
}
*
*
// |기준 문자열 파싱
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "ABO|OOO|ABCAB|C";
string table[4];
int tn = 0;
// '|' bar를 기준으로 문자열들을 파싱하여
//각 테이블 index에 순서대로 채워넣기
//table[0] = ABO
//table[1] = OOO
//...
int a = 0;
int b = 0;
str += "|";
while (1) {
b = str.find('|', a);
if (b == -1) break;
int size = b - a;
table[tn] = str.substr(a, size);
tn++;
a = b + 1;
}
return 0;
}
*
*
//////////////////////////문자열변환 (미완) 유튜브 다시보기
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "ABCOOOABCOOOOOOABCABC";
//ABC 다 찾아서
//KFCWORLD 라는 문자열로 전부 교체 replace
int a = 0;
//int sum=0;
int b = 0;
while (1)
{
a = str.find('ABC', a);
if (a == -1) break;
str.erase(a, 2);
str.insert(a,"KFCWORLD");
a += 8;
}
cout << str;
//괄호 안에 있는 수들의 전체 합 출력 (정답 : 9,555)
return 0;
}
*
*
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "QWE[1234]TR[1000][3000]BBQ[4321]AA";
//괄호 안에 있는 수들의 전체 합 출력 (정답 : 9,555)
int a = 0;
int b = 0;
int sum=0;
while (1)
{
a = str.find('[', a);
if (a == -1) break;
b = str.find(']', a + 1);
int size = b - a - 1;
string temp = str.substr(a + 1, size);
int n = stoi(temp);
sum += n;
a = b + 1;
}
cout << sum;
return 0;
}
*
#include <iostream>
using namespace std;
int main()
{
string str = "ABCDEF[ABCD]ABCDE[ERW]QQ[RRTYU]QQ[Q]";
//괄호 안에 있는 [ ] 문자열을 파싱하여서
//1. 한 문자열로 합치기 (+)
//2. 역순으로 출력 (for 출력)
//ABCDERWRRTYU
//UYTRRWREDCBA
int a = 0;
int b = 0;
string sum;
while (1)
{
a = str.find('[', a);
if (a == -1) break;
b = str.find(']', a + 1);
int size = b - a - 1;
string temp = str.substr(a + 1, size);
sum += temp;
a = b + 1;
}
for (int i = sum.size() - 1; i >= 0; i--) {
cout << sum[i];
}
return 0;
}
*
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "ABCDEF[1234]ABCDE";
//열기 괄호 [ 와
//닫기 괄호 ] 사이에 있는 문자열을 추출(파싱)
//출력 find 메서드 substr 메서드 사용하시면 됩니다.
int a = str.find('[');
int b = str.find(']', a + 1);
int size = b - a - 1;
string ret = str.substr(a + 1, size);
int n = stoi(ret);
//int sum = 0;
//for (int i = 0; i < ret.size(); i++) {
// sum = sum * 10 + (ret[i] - '0');
//}
//cout << sum;
return 0;
}
*
#include <cstring> //strcmp strlen 들어있는 "C언어" Lib
#include <string.h> //strcmp strlen 들어있는 "C언어" Lib
#include <string> //strcmp strlen 대신 string class를 쓸때 사용하는 Lib
using namespace std;
*
#include <iostream>
#include <cstring>
using namespace std;
string str = "ABCDABCEABCABCABABCDE";
int main()
{
//ABC가 총 몇개있는지 출력하는 프로그램
int a = 0;
int cnt = 0;
while (1)
{
int ret = str.find("ABC", a);
if (ret == -1) break;
cnt++;
a = ret + 1;
}
cout << cnt;
return 0;
}
*
//패턴찾기
#include <iostream>
using namespace std;
//string 을 안쓰고 푸는 문제
char vect[100] = "AKJSDKAJKLALAJKLASDLKASJLKASJLD";
char target[10] = "AJK";
//몇개가 있는지 검색하는 문제
//Library 없이 구현 (string.h 도 안씀)
int tn;
int isSame(int index)
{
for (int i = 0; i < tn; i++) {
if (vect[index + i] != target[i]) return 0;
}
return 1;
}
int main()
{
tn = strlen(target);
int n = strlen(vect);
int cnt = 0;
for (int i = 0; i <= n - tn; i++) {
int ret = isSame(i);
if (ret == 1) cnt++;
}
//strcmp사용(not 필수)
//for (int i = 0; i <= n - tn; i++) {
// if (!strncmp(&vect[i], target, tn)) cnt++;
//}
cout << cnt;
return 0;
}
*
#include <iostream>
#include <string>
using namespace std;
int main()
{
//아이디랑 pass를 입력받음
//아이디가 "good" password가 "1234" 이면
//"로그인성공"
//아이디는 맞는데 비번이 틀리면
//"비번 노노"
//아이디가 틀렸으면
//"그런사람 없어요"
string id;
string pass;
cin >> id >> pass;
//if (!strcmp(id, "good") && !strcmp(pass, "1234"))
if (id == "good" && pass == "1234") {
cout << "로그인성공";
}
else if (id != "good") {
cout << "그런사람 없어요";
}
else if (id == "good" && pass != "1234") {
cout << "비번 ㄴㄴ";
}
return 0;
}
*
//strcmp와 string비교
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "ABCDE";
string abc = "QWE";
string bbq = str + abc;
if (str == abc) {
cout << "비교";
}
else {
cout << "ㄴ";
}
char buf[10] = "ABCDE";
char buf2[10] = "ABCDE";
if (strcmp(buf, buf2) == 0) {
cout << "같음";
}
if (!strcmp(buf, buf2)) {
cout << "같음";
}
return 0;
}
*
// 문자열 거꾸로 출력
strig str;
cin>>str;
for(int i=str.size() -1; i>=0; i--){
cout <<str[i] <<" ";
}
*
//string 기본 사용 방법
#include <iostream>
#include <string>
using namespace std;
int main()
{
//C++ 의 문자열 다루는 방법
string str = "ABCDE";
//printf("%s", str.c_str());
cout << str << "\n";
for (int i = 0; i < str.size(); i++) {
cout << str[i] << " "; //char 배열처럼 사용 가능
}
return 0;
}
*
#include <iostream>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
char vect[10] = "ABCDE";
char vect2[10];
APS (P.S)
//문자열 P.S 문제
//간단한것은 char 로 처리
//복잡한것은 string class (C++) 을 쓰면
//문자열을 쉽게 처리할 수 있음
//P.S할때는 strlen, strcpy, strcat, strstr 적게쓰는 편
//임베디드 개발할때는 c언어 string.h 로그관련 처리에서 무진장 씁니다.
string class
strlen
strcpy
strcat
strstr
return 0;
}
*
#include <iostream>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
char vect[10] = "ABCDE";
char vect2[10];
//strcpy(vect2, vect); < memcpy를 더 많이씀
memcpy(vect2, vect, 10);
cout << vect2;
return 0;
}
*
include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
int main()
{
//문자열 다룰때 기본내용 정리
//3분 컷
//1. 초기화 방법
char vect[5] = { 'A', 'B', 'C', 'D', 'E' }; //널문자 추가 안됨 = 5개필요
char vect2[6] = "ABCDE"; //널문자가 자동으로 추가됨 = 6개 필요
int n = strlen(vect2);
cout << n;
//이게 안좋은 코딩인 이유? - O(N^2) 처럼 동작
for (int i = 0; i < strlen(vect2); i++) {
cout << vect2[i] << " ";
}
//이게 O(N)과 같이 동작
int vn = strlen(vect2);
for (int i = 0; i < vn; i++) {
cout << vect2[i] << " ";
}
return 0;
}
*
#include <iostream>
using namespace std;
int main()
{
//MessageBoxW(NULL, L"후후",L"키키",NULL);
char ch;
cin >> ch; //'B'
if (ch >= 'A' && ch <= 'Z') {
cout << (char)(ch - 'A' + 'a');
}
else if (ch >= 'a' && ch <= 'z') {
for (char x = 'A'; x <= 'Z'; x++) {
cout << x;
}
cout << "\n";
for (char x = 'Z'; x >= 'A'; x--) {
cout << x;
}
}
return 0;
}
'Algorithm > C++' 카테고리의 다른 글
[21.08.03] dat활용 counting sort와 vector사용법 (0) | 2021.08.03 |
---|---|
[21.08.02] 재귀함수2/ 백트래킹 (0) | 2021.08.02 |
[21.07.30] 재귀함수, 트리, DAT (0) | 2021.08.01 |
[21.07.29]스택, 큐 (0) | 2021.08.01 |
[21.07.26~27] APS 시간복잡도, 배열 기초 (0) | 2021.07.31 |