Skip to content

Instantly share code, notes, and snippets.

@nikAizuddin
Created December 6, 2014 16:39
Show Gist options
  • Save nikAizuddin/165731375956df76f582 to your computer and use it in GitHub Desktop.
Save nikAizuddin/165731375956df76f582 to your computer and use it in GitHub Desktop.
Convert integer to string without using any library
//////////////////////////////////////////////////////////////////////
// //
// Convert integer to string without using any library. //
// //
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str(4u,' ');
int number = 1993;
int digit0 = 0;
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
digit3 = (number - (((number / 10) * 10) ) ) ;
digit2 = (number - (((number / 100) * 100) + digit3) ) / 10;
digit1 = (number - (((number / 1000) * 1000) + digit2) ) / 100;
digit0 = (number - (((number / 10000) * 10000) + digit1) ) / 1000;
str.at(0) = digit0 + 0x30;
str.at(1) = digit1 + 0x30;
str.at(2) = digit2 + 0x30;
str.at(3) = digit3 + 0x30;
cout << str << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment