Thread: urgent help needed with itoa

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    212

    urgent help needed with itoa

    Hi

    I need help with implementing itoa function as below. What is wrong with my below code? It has compilation errors.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    void itoa (const int input, char* output){
    int i, sign;
    
    if((sign = input) <0)
    input = -input;
    i =0;
    do{
    output[i++] = input %10 + '0';  
    }while ((input /= 10) > 0);
    if(sign <0)
    output[i++] = '-';
    output[i] = '\0';
    reverse(output);
    }
    
    int main (){
    char buf[13];
    itoa(-23456, buf);
    printf("My Output: %s\n", buf);
    
    }
    Last edited by leo2008; 2 Weeks Ago at 12:37 PM.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    Below is the reverse function logic.

    Code:
    void reverse(char output[])
     {
         int i, j;
         char c;
     
         for (i = 0, j = strlen(output)-1; i<j; i++, j--) {
             c = output[i];
             output[i] = s[j];
             output[j] = c;
         }
     }

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    I changed code logic as below, but getting compile errors. what is wrong here?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    void itoa (const int input, char* output, int rad){
    int i = 0, n,sign;
    n = sign;
    
    while (n > 0)
    {
       sign = n%rad;
       n = n/rad;
       output[i++] = '0' + sign;
    }
    output[i] = '\0';
    strrev(output);
    }
    
    
    int main (){
    char output[13];
    itoa(726, output, 13);
    printf("\n %s  \n", output);
    return 0;
    
    }
    main.c:24:1: warning: implicit declaration of function ‘strrev’; did you mean ‘strsep’? [-Wimplicit-function-declaration]
       24 | strrev(output);
          | ^~~~~~
          | strsep
    /usr/bin/ld: /tmp/ccFVXKei.o: in function `itoa':
    main.c:(.text+0x79): undefined reference to `strrev'
    collect2: error: ld returned 1 exit status

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    Modified code as below, but has compile errors.. what is wrong here?

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #define true 1
    #define false 0
    
     
    void reverse(char output[], int len)
    {
        int start, end;
        char temp;
        for(start=0, end=len-1; start < end; start++, end--) {
            temp = *(output+start);
            *(output+start) = *(output+end);
            *(output+end) = temp;
        }
    }
     
    void itoa (const int input, char* output, int rad){
    int i = 0;
    bool isnegative = false;
     
    if(input == 0) {
    output[i] = '0';
    output[i + 1] = '\0';
    return output;
    }
     
    if(input < 0 && rad == 10){
    bool isnegative = true;
    input = -input;
    }
     
    while (input != 0){
      int rem = input%rad;
      output[i++] = (rem > 9) ? (rem-10) + 'A' : rem + '0';
      input = input/rad;
    }
     
    if(isnegative){
    output[i++] = '-';
    }
     
    output[i] = '\0';
    reverse(output, i);
    return output;
    }
     
     
    int main (){
     
    int i, b;
    char array[13];
    printf("Enter a number and base\n");
    scanf("%d %d", &i, &b);
          
    //printf("String : %s", itoa(i, array, b));
    //return 0;   
     
    }
    
    main.c:33:8: warning: ‘return’ with a value, in function returning void
       33 | return output;
          |        ^~~~~~
    main.c:26:6: note: declared here
       26 | void itoa (const int input, char* output, int rad){
          |      ^~~~
    main.c:38:7: error: assignment of read-only parameter ‘input’
       38 | input = -input;
          |       ^
    main.c:44:9: error: assignment of read-only parameter ‘input’
       44 |   input = input/rad;
          |         ^
    main.c:53:8: warning: ‘return’ with a value, in function returning void
       53 | return output;
          |        ^~~~~~
    main.c:26:6: note: declared here
       26 | void itoa (const int input, char* output, int rad){
          |      ^~~~
    Last edited by leo2008; 2 Weeks Ago at 01:47 PM.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    can anyone help me? i am stuck with this code.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    886
    Why "reverse" the buffer if you can fill it backwards?

    Code:
    #include <stdio.h>
    
    static int digits10( unsigned int n )
    {
      static const unsigned int vals[] =
        { 10U, 100U, 1000U, 10000U, 100000U, 1000000U,
          10000000U, 100000000U, 1000000000U };
      int i;
    
      if ( n >= 1000000000U ) return 10;
    
      i = 0;
      while ( i < sizeof vals / sizeof vals[0] )
        if ( n < vals[i++] ) 
          break;
    
      return i;
    }
    
    char *itoa_( int n, char *str )
    {
      char *endp;
      unsigned int m;
      int signal;
    
      signal = n < 0;
    
      m = signal ? -n : n;
    
      // Points to the end of the string.
      endp = str + digits10( m ) + signal;
    
      *endp-- = '\0';
      do 
        *endp-- = '0' + m % 10;
      while ( m /= 10 );
    
      if ( signal ) *endp = '-';
    
      return str;
    }
    
    int main( void )
    {
      char buff[12];
    
      printf( "%s\n", itoa_( -726, buff ) );
    }

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    As per your logic, it output as -726. How can I used void function to implement itoa? I need to use below signature.

    Code:
    void itoa (const int input, char* output) {}

  8. #8
    Registered User
    Join Date
    Apr 2021
    Posts
    54
    The only errors I saw on your original code were these:

    Code:
    $ gcc -Wall -Wextra xx.c
    xx.c: In function ‘itoa’:
    xx.c:5:7: error: assignment of read-only parameter ‘input’
     input = -input;
           ^
    xx.c:9:16: error: assignment of read-only parameter ‘input’
     }while ((input /= 10) > 0);
                    ^~
    xx.c:13:1: warning: implicit declaration of function ‘reverse’ [-Wimplicit-function-declaration]
     reverse(output);
     ^~~~~~~
    It seems like you need to not declare your input parameter as const, since you are modifying it. And you need to provide a definition or declaration of the reverse function.

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    886
    Quote Originally Posted by leo2008 View Post
    As per your logic, it output as -726
    As it should.
    How can I used void function to implement itoa? I need to use below signature.

    Code:
    void itoa (const int input, char* output) {}
    Simple. Change the code.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    How to change code logic to handle negative to positive? I mean change -726 to +726. In your itoa function, something like this?

    Code:
     
    if((signal = n)<0)//record signal
    n=-n;//make n a positive number

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    I have removed the const input and below is my code. please check it.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #define true 1
    #define false 0
    
     
    void reverse(char output[], int len)
    {
        int start, end;
        char temp;
        for(start=0, end=len-1; start < end; start++, end--) {
            temp = *(output+start);
            *(output+start) = *(output+end);
            *(output+end) = temp;
        }
    }
     
    void itoa (int input, char* output, int rad){
    int i = 0;
    bool isnegative = false;
     
    if(input == 0) {
    output[i] = '0';
    output[i + 1] = '\0';
    //return output;
    }
     
    if(input < 0 && rad == 10){
    bool isnegative = true;
    input = -input;
    }
     
    while (input != 0){
      int rem = input%rad;
      output[i++] = (rem > 9) ? (rem-10) + 'A' : rem + '0';
      input = input/rad;
    }
     
    if(isnegative){
    output[i++] = '-';
    }
     
    output[i] = '\0';
    reverse(output, i);
    return output;
    }
     
     
    int main (){
     
    int i, b;
    char array[13];
    printf("Enter a number and base\n");
    scanf("%d %d", &i, &b);
          
    //printf("String : %s", itoa(i, array, b));
    //return 0;   
     
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,736
    > I have removed the const input and below is my code. please check it.
    How about you indent it properly first.
    It's a dogs breakfast.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    leo2008:

    You also #include stdbool.h, but then redefine "true" and "false" which are already defined in stdbool.h!

    Compile with all warnings turned on and see the results!

    Have you actually tested the code yourself?

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    If I remove stdbool.h, I get the below errors.

    Code:
    main.c:28:1: error: unknown type name ‘bool’
       28 | bool isnegative = false;
          | ^~~~
    main.c:37:1: error: unknown type name ‘bool’
       37 | bool isnegative = true;
          | ^~~~

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    212
    I have modified the below code and tested output below. But it doesnt print any string values after providing number and base. Can someone help me understand what is wrong in this code?

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #define true 1
    #define false 0
    
     
    void reverse(char output[], int len)
    {
        int start, end;
        char temp;
        for(start=0, end=len-1; start < end; start++, end--) {
            temp = *(output+start);
            *(output+start) = *(output+end);
            *(output+end) = temp;
        }
    }
     
    void itoa (int input, char* output, int rad){
    int i = 0;
    bool isnegative = false;
     
    if(input == 0) {
    output[i] = '0';
    output[i + 1] = '\0';
    //return output;
    }
     
    if(input < 0 && rad == 10){
    bool isnegative = true;
    input = -input;
    }
     
    while (input != 0){
      int rem = input%rad;
      output[i++] = (rem > 9) ? (rem-10) + 'A' : rem + '0';
      input = input/rad;
    }
     
    if(isnegative){
    output[i++] = '-';
    }
     
    output[i] = '\0';
    reverse(output, i);
    //return output;
    }
     
     
    int main (){
     
    int i, b;
    char array[13];
    printf("Enter a number and base\n");
    scanf("%d %d", &i, &b);
          
    //printf("String : %s", itoa(i, array, b));
    //return 0;   
     
    }
    
    
    OUTPUT:
    
    Enter a number and base
    10 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent, help needed.
    By sinnclaus in forum C Programming
    Replies: 4
    Last Post: 03-29-2010, 06:08 AM
  2. Urgent Help needed!
    By Superstar90 in forum C++ Programming
    Replies: 3
    Last Post: 11-10-2009, 04:21 PM
  3. urgent help needed!!!
    By yosef_yaniv in forum C++ Programming
    Replies: 5
    Last Post: 12-08-2007, 12:36 PM
  4. Urgent! Help Needed
    By DarkManiac in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2004, 07:16 PM
  5. urgent help needed
    By david in forum C Programming
    Replies: 0
    Last Post: 11-27-2001, 08:27 AM

Tags for this Thread