Need a project done?

C++ Programming Developer

Search This Blog

How to Convert Int to String in C++

I've created my own function to convert an integer (int) to String in C++.

#include "stdafx.h"
#include <string>
#include <iostream>
#include <stack>

using namespace std;

string intToString(int no);

int main(int argc, _TCHAR* argv[])
{
cout << "These are all strings numbers" << endl;
for (int i = 0; i < 20; i++)
cout << intToString(i) << endl;
}

string intToString(int no){
string s = "";
string s2 = "";
if (no <= 9 && no >= 0){
char c = no + 48;
s += c;
return s;
}
else{
do{
char c = no % 10;
s += c + 48;
no /= 10;
} while (no != 0);
//Reverse string
for (int i = s.length() - 1; i >= 0; i--)
{
s2 += s[i];
}
return s2;
}
}

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."