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

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…

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’;

1 Like

Read General Method in this editorial of problem from recently held cook off: ONOZ - Editorial - editorial - CodeChef Discuss

1 Like

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;

}

1 Like

char buf[100]; // 100 = max digits in number
sprintf(buf, “%lli”, num); // num is a long long integer

2 Likes

i use stringstream to convert between int and string in c++

#include <iostream>
#include <sstream>
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;
}
3 Likes

You could try something like this in C:

#include <stdio.h>
#include <string.h>

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
1 Like

In Java, simplest way to convert

int number = 10;
String text=""+number;
System.out.println(text);//prints 10 

Thanks for providing various solutions. Our universe is meant to be extended , thanks for helping in extending mine.