Append int to end of C string

Posted on 2003-02-28
Medium Priority
Last Modified: 2010-04-17
I have a loop that counts 1 to 25. I want to append the number (I) from the loop to the end a string. I can do it like str[len] = '1', but it I do str[len] = i, I get some kind of weird character. Any ideas? thanks.
Question by:010101010101
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8046674
If this is not homework I will give you an example, but in general look up

//i is the number to convert
//dest is the destination char buffer
//radix is the base of the number (10 for example)
char * itoa(int i, char * dest, int radix)

to convert your int to a string

and then use strcat(char * dest, const char * src)

you will end up with a string with dest+src

good luck.

Author Comment

ID: 8048180

Thanks for the help, unfortunately itoa is not ansi c, and does not work with my compiler (GCC 3.2.1 FreeBSD). I looked in the header files, and could only find referances to atoi (the opposite of what I want). Any other ideas? I tried toascii, but had no success. I also tried to cast the int as a char but that did not work either. Thanks.

Author Comment

ID: 8048290
Ok. figured it out. I simply added 48 to the number (ascii table) I wanted, then set strlen = number, then strlen+1 = '\0'. Thanks.

Accepted Solution

TheBeaver earned 225 total points
ID: 8049045
Thats fine for 0-9, but you asked about 0-25. So try this...

for(x=0; x<25; x++)
  sprintf(str, "%s %d", x);

or if you dont want the 10s digit...

for(x=0; x<25; x++)
  sprintf(str, "%s%d", x % 10);


Expert Comment

ID: 8054757
int i;
int slen;  /*The length of you string*/

slen = strlen(yourstring);

for (i=1 ; i<=25 ; i++)
  if (i>=10)  { /*We have to deal with 2 digits*/
    yourstring[slen]= (char)(i/10) + '0'; /*Tens digit*/
    yourstring[slen+1]= (char)(i%10) + '0'; /*Ones digit*/
    yourstring[slen+2]= '\0'; /*Null terminate string*/
  else { /*We only have to deal with 1 digit here*/
    yourstring[slen]= (char)(i%10) + '0'; /*Ones digit*/
    yourstring[slen+1]= '\0';/*Null terminate string*/

  printf("%s\n",yourstring); /*Print yourstring to screen*/


NOTE: You have to be sure that the size of the yourstring character buffer is large enough to hold the extra characters that you are appending at the end.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question