[C++] 숫자, 문자 변환

Zeung-il Kim
Oct 25, 2021

--

medium에서 code block 입력 : ctrl+alt+6

코테에서 숫자를 문자로 바꾸거나 반대로 문자를 숫자로 바꿔야 되는 경우가 빈번한데, 이 경우 유용한 방법임.

#include <bits/stdc++.h>using namespace std;int main()
{
int num;
cin>>num;
string str = to_string(num);
string str2 = "BK12";
for(int i=0; i<str.size(); i++) cout<<str[i]<<'\n';

int a = str[0] - '0';
int b = str[1] - '0';
cout<<a+b<<'\n';

cout<<isdigit(str[0])<<'\n';
cout<<isdigit(str[1])<<'\n';
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response