Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to implement the itoa() function to convert int to string in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>
  
#define LEN 10
  
void my_itoa(int n, char s[]);
  
int main(void)
{
    char s[LEN];
    int n = 23479;
      
    my_itoa(n, s);
    printf("s = %s\n", s);
     
    return 0;
}
 
void my_itoa(int n, char s[]) 
{ 
    int i, sign = n; 
     
    if (n < 0) 
        n = -n;         
    i = 0; 
    do
    {     
        s[i++] = n % 10 + '0';  
    } while ( (n /= 10) > 0);   
        
    if (sign < 0) 
        s[i++] = '-'; 
    s[i] = '\0'; 
     
    strrev(s); 
} 
  
/*
run:
    
s = 23479
 
*/

 



answered Nov 10, 2015 by avibootz
edited Nov 11, 2015 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
  
#define LEN 13
  
void my_itoa(int n, char s[]);
  
int main(void)
{
    char s[LEN];
    int n = -87423;
      
    my_itoa(n, s);
    printf("s = %s\n", s);
     
    return 0;
}
 
void my_itoa(int n, char s[]) 
{ 
    int i, sign = n; 
     
    if (n < 0) 
        n = -n;         
    i = 0; 
    do
    {     
        s[i++] = n % 10 + '0';  
    } while ( (n /= 10) > 0);   
        
    if (sign < 0) 
        s[i++] = '-'; 
    s[i] = '\0'; 
     
    strrev(s); 
} 
  
/*
run:
    
s = -87423
 
*/

 



answered Nov 10, 2015 by avibootz
edited Nov 11, 2015 by avibootz

Related questions

1 answer 166 views
1 answer 107 views
107 views asked Dec 7, 2022 by avibootz
1 answer 76 views
76 views asked Dec 7, 2022 by avibootz
1 answer 111 views
1 answer 105 views
1 answer 209 views
1 answer 209 views
...