You are not logged in. Please login at www.codechef.com to post your questions!

×

# How to convert an interger to string with any stl function ?

 0 Like I have an integer stored in a variable and wanted to convert it into string without c++ stl to_String() . How can it be done ? Thanks In Advance.... This question is marked "community wiki". asked 21 Mar '16, 00:44 240●3●9●32 accept rate: 5% Thanks for providing various solutions. Our universe is meant to be extended , thanks for helping in extending mine. (15 Apr '16, 14:22)

 3 i use stringstream to convert between int and string in c++ #include #include using namespace std; string int_to_str(int num) { stringstream ss; ss << num; return ss.str(); } int str_to_int(string str) { stringstream ss(str); int num; ss >> num; return num; } int main() { int n = 25; string str = "256"; cout << "int " << n << " converted to string " << "\"" << int_to_str(n) << "\"" << endl; cout << "string " << "\"" << str << "\"" << " converted to int " << str_to_int(str) << endl; return 0; }  answered 11 Apr '16, 01:12 226●2●9 accept rate: 9%
 2 char buf; // 100 = max digits in number sprintf(buf, "%lli", num); // num is a long long integer  answered 10 Apr '16, 21:37 21●1 accept rate: 0%
 1 if you asked because of march cook-off , there is better approach without string processing and if not convert integer into string Without any library function: i=0; while(num) { str[i++]=num%10; num=num/10; } str[i]='\0'; answered 21 Mar '16, 00:51 114●5 accept rate: 16%
 1 Read General Method in this editorial of problem from recently held cook off: https://discuss.codechef.com/questions/80151/onoz-editorial answered 21 Mar '16, 00:54 6★vsp4 1.2k●1●3●8 accept rate: 28%
 1 string convert(int a){ string b = ""; int d; while(a!=0){ d=a%10; a/=10; switch(d){ case 1: b.append("1");break; case 2: b.append("2");break; case 3: b.append("3");break; case 4: b.append("4");break; case 5: b.append("5");break; case 6: b.append("6");break; case 7: b.append("7");break; case 8: b.append("8");break; case 9: b.append("9");break; case 0: b.append("0");break; } } return b; } answered 10 Apr '16, 20:59 4★ay2306 232●9 accept rate: 11%
 1 You could try something like this in C: #include #include char *convert(char *dest, size_t size, int numer); static char *buferSizeCheck(char *array, size_t size, int numer); int main(void){ size_t size = 20; char array[size]; int numer = 125; convert(array, size, numer); printf("Array = %s\nLength = %zu\n",array,strlen(array)); } static char *buferSizeCheck(char *array, size_t size, int numer) { if (size == 0) { return NULL; } if (numer <= -10) { array = buferSizeCheck(array, size - 1, numer / 10); if (array == NULL) return NULL; } *array = (char) ('0' - numer % 10); return array + 1; } char *convert(char *array, size_t size, int numer) { char *ptr = array; if (size == 0) { return NULL; } size--; if (numer < 0) { if (size == 0){ return NULL; } size--; *ptr++ = '-'; } else { numer = -numer; } ptr = buferSizeCheck(ptr, size, numer); if (ptr == NULL){ return NULL; } *ptr = 0; return array; }  Output: Array = 125 Length = 3  answered 11 Apr '16, 15:55 0★tara24 26●1 accept rate: 16%
 0 In Java, simplest way to convert int number = 10; String text=""+number; System.out.println(text);//prints 10 answered 15 Apr '16, 20:45 11●2 accept rate: 0%
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,918
×1,491
×1,314
×279
×100

question asked: 21 Mar '16, 00:44

question was seen: 15,295 times

last updated: 15 Apr '16, 21:49