JUNGOL 기초다지기 14 문자열1
프로그래밍에서 가장 기초적인 내용을 다루는 기초 다지기입니다.
이번 포스팅은 기초 다지기 중 열네 번째인 문자열1 문제를 풀어보도록 하겠습니다.
기초 내용인 만큼 다른 설명없이 문제와 코드만 간단하게 작성하도록 하겠습니다.
593 : 문자열1 - 자가진단1

#include <iostream>
using namespace std;
bool IsInRange(short code);
int main()
{
short code;
while (true)
{
cout << "ASCII code =? ";
cin >> code;
if (IsInRange(code))
cout << static_cast<char>(code) << endl;
else
break;
}
}
bool IsInRange(short code)
{
if (33 <= code && code <= 127) return true;
return false;
}
594 : 문자열1 - 자가진단2

이 문제는 굉장히 어이없게도 string 클래스를 사용하면 바로 해결이 가능합니다.
당연히 string이 아니라 문자열 배열을 사용해야할 것으로 생각했는데 아니었나 보네요.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string data;
cin >> data;
cout << data << data << endl;
}
595 : 문자열1 - 자가진단3

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hong Gil Dong";
for (int i = 3; i <= 6; ++i)
cout << str[i];
cout << endl;
}
596 : 문자열1 - 자가진단4

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hong Gil Dong";
int var;
cin >> str >> var;
int strLength = str.length();
for (int i = strLength - 1; i >= strLength - var && i >= 0; --i)
cout << str[i];
cout << endl;
}
597 : 문자열1 - 자가진단5

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2;
cin >> str1 >> str2;
cout << str1.length() + str2.length() << endl;
}
598 : 문자열1 - 자가진단6

char형으로 입력받은 이후에 출력 시에 타입 케스팅하는 방법 외에 방법을 찾아보니 변수에 '+'를 붙이면 타입에 상관없이 숫자로 출력해 준다고 합니다.
#include <iostream>
#include <string>
using namespace std;
bool IsAlpha(char data);
bool IsNumber(char data);
int main()
{
char data;
while (true)
{
cin >> data;
if (IsAlpha(data))
cout << data << endl;
else if (IsNumber(data))
cout << +data << endl;
else
break;
}
}
bool IsAlpha(char data)
{
if ('A' <= data && data <= 'Z') return true;
if ('a' <= data && data <= 'z') return true;
return false;
}
bool IsNumber(char data)
{
if ('0' <= data && data <= '9') return true;
return false;
}
599 : 문자열1 - 자가진단7

#include <iostream>
#include <string>
using namespace std;
bool IsAlpha(char c);
int main()
{
string str;
cin >> str;
for (int i = 0; i < str.length(); ++i)
{
if(IsAlpha(str[i]))
cout << static_cast<char>(toupper(str[i]));
}
cout << endl;
}
bool IsAlpha(char c)
{
if ('A' <= c && c <= 'Z') return true;
if ('a' <= c && c <= 'z') return true;
return false;
}
600 : 문자열1 - 자가진단8

getline 함수를 사용하면 인풋 스트림과 string 변수를 파라미터로 넘겨주게 됩니다.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int blankCnt = 0;
for (int i = 0; i < str.length(); ++i)
{
if (str[i] == ' ')
blankCnt++;
}
cout << blankCnt + 1 << endl;
}
601 : 문자열1 - 자가진단9

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
int strLength = str.length();
for (int i = 1; i <= strLength; ++i)
{
for (int j = strLength - i; j < strLength; ++j)
cout << str[j];
for (int j = 0; j < strLength - i; ++j)
cout << str[j];
cout << endl;
}
}
182 : 문자열1 - 형성평가1

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char ca, cb;
cin >> ca >> cb;
short sa{ ca }, sb{ cb };
cout << sa + sb << ' ' << abs(sa - sb) << endl;
}
183 : 문자열1 - 형성평가2

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
cout << str.substr(0, 5) << endl;
}
184 : 문자열1 - 형성평가3

#include <iostream>
#include <string>
using namespace std;
bool IsAccessible(char c);
int main()
{
string str;
cin >> str;
for (int i = 0; i < str.size(); ++i)
{
if(IsAccessible(str[i]))
cout << static_cast<char>(tolower(str[i]));
}
cout << endl;
}
bool IsAccessible(char c)
{
if ('A' <= c && c <= 'Z') return true;
if ('a' <= c && c <= 'z') return true;
if ('0' <= c && c <= '9') return true;
return false;
}
185 : 문자열1 - 형성평가4

string::find 메소드는 string 내에서 해당하는 문자의 위치를 리턴하는 함수입니다.
그런데 만약 찾는 문자가 존재하지 않으면 string::npos를 리턴합니다.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
char c;
cin >> str >> c;
int pos = str.find(c);
if (pos != string::npos)
cout << pos << endl;
else
cout << "No" << endl;
}
186 : 문자열1 - 형성평가5

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2;
cin >> str1 >> str2;
cout << ((str1.size() > str2.size()) ? str1.size() : str2.size()) << endl;
}
187 : 문자열1 - 형성평가6

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
int pos;
while (str.size() > 1)
{
cin >> pos;
pos = pos > str.size() ? str.size() : pos;
str.replace(pos - 1, 1, "");
cout << str << endl;
}
}
188 : 문자열1 - 형성평가7

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int prePos = 0, pos;
for (int i = 1; prePos < str.size(); ++i)
{
pos = str.find(' ', prePos);
if (pos == string::npos)
pos = str.size();
cout << i << ". " << str.substr(prePos, pos - prePos) << endl;
prePos = pos + 1;
}
}
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=10e0
JUNGOL | 문제은행 1 페이지
경기도 안양시 동안구 평촌대로 109 협성골드프라자 601호 TEL : 031-360-4144 FAX : 031-388-0996 E-mail : hancomc@hotmail.com, comkiwer@naver.com Copyrightⓒ 2010-2019 jungol. All right reserved. TOP
www.jungol.co.kr