1

I have code like this:

char str[100];
int r = 0;
for(int k = 0; k < i;k++){
    str[r++] = y[k];
    sprintf(str[r], str, x[k]);
    r++;
}

I want in array y I have only alphabetic characters(e.g C,D...) and in array x I have only numbers. I want to make string like "C50D80E20" etc." But I dont know how to put interger into string(I know I´m using sprintf wrong and also that it shouldn´t be used in this case).

Thanks in advance.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Does this answer your question? [How to convert an int to string in C?](https://stackoverflow.com/questions/8257714/how-to-convert-an-int-to-string-in-c) – Fred Larson Mar 12 '20 at 16:40
  • 2
    You can convert digit to char by adding '0'. For example: char y= 6+'0'; Makes y holding the char '6'. – YanBir Mar 12 '20 at 16:45
  • @YanBir in ASCII yes but generally not – 0___________ Mar 12 '20 at 16:52
  • @YanBir It only works for numbers less than 10 :/ I need somethig that can work for any number, but thanks for help. – ThomasCodesThings Mar 12 '20 at 16:53
  • @P__J__ What it means in generally? Could you give an example of not. (Considering the number is less then 10) – YanBir Mar 12 '20 at 16:55
  • 2
    @P__J__ *in ASCII yes but generally not* No, it works in any [C character set](https://port70.net/~nsz/c/c11/n1570.html#5.2.1p2): "In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous." But it only works for the `int` values `0` through `9`. – Andrew Henle Mar 12 '20 at 17:04

3 Answers3

3

Here you are.

#include <stdio.h>

int main(void) 
{
    enum { N = 100 };
    char s[N];

    char a[] = "CDE";
    int  b[] = { 50, 80, 20 };

    int pos = 0;
    for ( size_t i = 0; i + 1 < sizeof( a ); i++ )
    {
        pos += sprintf( s + pos, "%c%d", a[i], b[i] );
    }

    s[pos] = '\0';

    puts( s );

    return 0;
}

The program output is

C50D80E20

This statement

s[pos] = '\0';

is required only in the case when there are no values to append to the array s that is when none call of sprintf was executed.

If you want to get a string like this

C50 D80 E20

then just write for example

pos += sprintf( s + pos, "%c%d%c", a[i], b[i], ' ' );

And if you want to remove the last space character then instead of

s[pos] = '\0';

write

s[ pos == 0 ? pos : pos - 1 ] = '\0';

Instead of the function sprintf you could use the function snprintf. But it does not resolve the problem if you allocated not enough memory for the result string because in any case you will not get the expected result in such a case.

As for the function itoa then it is not a standard C function.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Great, what if want to add " " after each combination? Following code does not seem to work. char c = ' '; pos += sprintf( s + pos, "%c%d%c", a[i], b[i] , c); – ThomasCodesThings Mar 12 '20 at 17:08
  • @ThomasCodesThings I do not see any problem. Just use pos += sprintf( s + pos, "%c%d%c", a[i], b[i], ' ' ); That is use the character literal ' ' (not the string literal " "). – Vlad from Moscow Mar 12 '20 at 17:08
  • Whenever I do that program drops. – ThomasCodesThings Mar 12 '20 at 17:13
  • @ThomasCodesThings It seems you are doing something wrong. Maybe you incorrectly specified the condition in the loop. Or your array a contains less characters than the array size and so on. Just copy and paste my example. Pay attention to that you need to use the character literal ' ' not the string literal " ". – Vlad from Moscow Mar 12 '20 at 17:14
  • I slightly modiefied8your example and it works fine I just want to make final string like "C50 D80 E20" etc. – ThomasCodesThings Mar 12 '20 at 17:17
  • I had less memory allocated that I throught. That was the problem. – ThomasCodesThings Mar 12 '20 at 17:46
0

Use itoa(). Somthing like that:

itoa(y[k], str[r++], 10);

Here's a link about itoa().

UPDT:

Or as correctly commentator marked - you can use int + '0'

JuiceFV
  • 171
  • 11
0

Is there any proper way how to put integer into string? [C]

Use snprintf().
Check results.

char str[N];
int len = snprintf(str[r], sizeof str, "%d", x[k]);
if (len < 0 || (unsigned)len >= sizeof str) Handle_Error();
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256