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 ?

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

shubham99's gravatar image

2★shubham99
2403932
accept rate: 5%

wikified 15 Apr '16, 14:21

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

(15 Apr '16, 14:22) shubham992★

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;
}
link

answered 11 Apr '16, 01:12

atulshanbhag's gravatar image

4★atulshanbhag
22629
accept rate: 9%

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

answered 10 Apr '16, 21:37

neil_1996's gravatar image

1★neil_1996
211
accept rate: 0%

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

link

answered 21 Mar '16, 00:51

deepakmourya's gravatar image

4★deepakmourya
1145
accept rate: 16%

edited 21 Mar '16, 01:05

Read General Method in this editorial of problem from recently held cook off: https://discuss.codechef.com/questions/80151/onoz-editorial

link

answered 21 Mar '16, 00:54

vsp4's gravatar image

6★vsp4
1.2k138
accept rate: 28%

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;

}

link

answered 10 Apr '16, 20:59

ay2306's gravatar image

4★ay2306
2329
accept rate: 11%

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
link

answered 11 Apr '16, 15:55

tara24's gravatar image

0★tara24
261
accept rate: 16%

edited 11 Apr '16, 16:01

In Java, simplest way to convert

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

link

answered 15 Apr '16, 20:45

debverine's gravatar image

2★debverine
112
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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