Created
December 6, 2014 16:39
-
-
Save nikAizuddin/165731375956df76f582 to your computer and use it in GitHub Desktop.
Convert integer to string without using any library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////// | |
// // | |
// 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