How To Convert Integer To String in C++




Supppose I have an integer= 0123456789101112 .  Now This Integer Can be Converted into String By stringstream class

Here is Code in C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;
    string s;
    stringstream st;
    for(i=0;i<=12;i++)
    {
        st<<i;
    }
    s=st.str();
    cout<<s<<endl;
    return 0;

}


Code To Find Desire Position String :

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;
    string s;
   // cout<<"Enter Position"<<endl;
    cin>>n;
    stringstream st;
    for(i=0;i<=1000;i++)
    {
        st<<i;
    }
    s=st.str();
    cout<<s[n]<<endl;
    return 0;


}


Related Problem : Codeforces 352 Div 2 A




Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment