dario3004
dario3004

Reputation: 21

int to string conversion not working properly

I'm trying to create a function that converts an integer into a string, basically what have I done is the following functions: when we get the numbers from the conversion they are reversed so I need a reverse function to make them in the right way. The intostring uses (I think? I got it from some website) the ascii table to convert the number into the string desired.

The problem is: when I enter the 2-digit number they are reversed the wrong way (I guess my reverse function doesn't work that well) and after a certain number of digit the conversion it's not any more accurate.

reverse function:

char reverse(char *stringa) {
    int len = strlen(stringa) - 1;
    for(int i = 0; i < len / 2; i++) {
        char tmp = stringa[i];
        stringa[i] = stringa[len - i];
        stringa[len - i] = tmp;
    }
}

intostring function:

void intostring(int num, char *str) {
    int i = 0;
    while (num != 0) {
        int rem = num % 10;
        str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
        num = num / 10;
    }
    str[i] = '\0';
    reverse(str);
}   

Upvotes: 1

Views: 345

Answers (5)

Badhan Sen
Badhan Sen

Reputation: 737

The reverse function condition is worng.

If the integer in 32 then the string will be s[0] = '2', s[1] = '3', s[2] = '\0' before string reversal. so in reverse function the following swap operation has to be applied as

if number = 32 then len = 2
i = 0 then len - i - 1 = 1
so 0 and 1 will be swaped.
void reverse(char *stringa){
    int len = strlen(stringa);
    for(int i = 0; i < len / 2; i++){
        char tmp = stringa[i];
        stringa[i] = stringa[len - i - 1];
        stringa[len - i - 1] = tmp;
    }
}
void intostring(int num, char *str)
{
    int i = 0;
    if(num == 0){
        str[i++] = '0';
        str[i] = '\0';
    }
    else if(num > 0){
        while(num != 0){
            int rem = num % 10;
            str[i++] = '0' + rem;
            num = num/10;
        }
        str[i] = '\0';
    }
    else{
        while(num != 0){
            int rem = num % 10;

            /*
                (-5/2) => -2
                -2 * 2 => -4
                so a%b => -1

                (5/-2) => -2
                -2 * -2 => 4
                so a%b => 1

            */

            rem = abs(rem); // as the rem value is negative
            str[i++] = '0' + rem;
            num = num/10;
        }
        str[i++] = '-';
        str[i] = '\0';
    }
    reverse(str);
}

Upvotes: 1

Fractal
Fractal

Reputation: 814

Your reverse function is wrong. You can make it correct (and readable) like below:

void reverse(char *stringa) {
    int i = 0; //Forwarding moving index
    int j = strlen(stringa) - 1; // the end index
    int tmp; // Edited to get the code compiled
    for (; i < j; i++, j--) {
        tmp        = stringa[i];
        stringa[i] = stringa[j];
        stringa[j] = tmp;
    }
}

Upvotes: 0

chqrlie
chqrlie

Reputation: 145297

There are multiple problems:

  • the reverse function fails for an empty string. You should not subtract 1 from the length, but adjust the offset inside the loop.

  • reverse is defined to return a char but does not return anything. Make it return a char * and return stringa.

  • intostring produces an empty string for num <= 0. You should loop while num > 9 and add the final digit after the loop.

  • intostring converts the digit into a character for bases up to 36 (assuming ASCII). This is unnecessarily complex since the base is 10. Use a simpler conversion: str[i++] = '0' + rem;

  • it may be useful for intostring to return a pointer to the destination array.

Here is a modified version:

#include <string.h>

char *reverse(char *str) {
    size_t len = strlen(str);
    for (size_t i = 0; i < len / 2; i++) {
        char tmp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = tmp;
    }
    return str;
}

char *intostring(int num, char *str) {
    int i = 0;
    if (num >= 0) {
        while (num > 9) {
            str[i++] = '0' + num % 10;
            num = num / 10;
        }
        str[i++] = '0' + num;
    } else {
        while (num < -9) {
            str[i++] = '0' - num % 10;
            num = num / 10;
        }
        str[i++] = '0' - num;
        str[i++] = '-';
    }
    str[i] = '\0';
    return reverse(str);
} 

Here is an alternative approach for the reverse function using 2 index variables:

#include <string.h>

char *reverse(char *str) {
    size_t i = 0;
    size_t j = strlen(str);
    while (j --> i) {
        char c = str[j];
        str[j] = str[i];
        str[i++] = c;
    }
    return str;
}

Upvotes: 1

Генс
Генс

Reputation: 93

 /*
It works clearly . Checked.
*/
void  reverse(char source[],char destination[]) {
   int x,i;
   //start from last char
   i = i=(strlen(source)-1
   for (x=0;x<strlen(source);x++){
     //Insert char at i in source to x in destination
     destination[x]=source[i];
     destination[x]='\0';
     i--;
    }
}

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

The condition i<len/2 in the reverse function is wrong.

For example, if the string is 2-digit long, len will be 1 and len/2 will be 0. Therefore, no swap will occure while the two characters should be swapped.

the condition should be i<=len/2 or i<len-i.

Upvotes: 3

Related Questions