How to put an integer at the end of a string?

0

I need to loop through n by putting an integer at the end of a string.

Ex: string "E0" and an integer from 0 to n , something like this:

  

E00, E02, E03, ..., E0n

char ent[3] = {'E','0'};
char a[1];
int n=5;//exemplo

for (int i = 0; i < n; i++) {
        itoa(i,a,10);
       strcat(ent,a);
            printf("novo ent: %s \n ",ent);
        }

I do not understand what is wrong, because you are only printing the variable "a" and not "ent+a" .

    
asked by anonymous 18.11.2018 / 23:44

2 answers

1

Just to give more possibilities to the solutions presented by @Isac.

Alternate use of sprintf

int main() {
    char prefixo[3] = {'E','0'};
    char ent[4];//se quiser números com 2 casas decimais, basta por tamanho 5
    int n=5;//exemplo

    for (int i = 0; i < n; i++) {
        sprintf(ent, "%s%d", prefixo, i); //sprintf em vez de itoa
        printf("novo ent: %s \n ",ent);
    }
    return 0;
}

Direct character writing

You can simply compute the desired character and put it in position 2 of the string:

int main() {
    char ent[3] = {'E','0', '
int main() {
    char prefixo[3] = {'E','0'};
    char ent[4];//se quiser números com 2 casas decimais, basta por tamanho 5
    int n=5;//exemplo

    for (int i = 0; i < n; i++) {
        sprintf(ent, "%s%d", prefixo, i); //sprintf em vez de itoa
        printf("novo ent: %s \n ",ent);
    }
    return 0;
}
', '
int main() {
    char ent[3] = {'E','0', '%pre%', '%pre%'};
    char num;
    int n=5;//exemplo

    for (int i = 0; i < n; i++) {
        num = '0' + i; // calculando o caracter a partir de um offset i do caracter '0'
        ent[2] = num;
        printf("novo ent: %s \n ",ent);
    }
    return 0;
}
'}; char num; int n=5;//exemplo for (int i = 0; i < n; i++) { num = '0' + i; // calculando o caracter a partir de um offset i do caracter '0' ent[2] = num; printf("novo ent: %s \n ",ent); } return 0; }
  

this solution was designed to single-digit numbers, you can do it better

    
19.11.2018 / 01:24
1

The first problem that causes the code to not work is not allocating space for the terminator in the string that passes to itoa . Remember that the result is a correctly terminated string as soon as you have at least two characters, what you put in and the terminator.

So char a[1] must be at least char a[2] . The itoa itself is something that you should not use since it is not standard and therefore not available in all compilers, like mine. You should instead use sprintf which is easy in it and is standard .

The other problem is that each concatenation is permanent over the string ent . That's why your texts are growing: E00 , E001 , E0012 , etc. Each iteration of for must start in the original string, which can be done by duplicating the original with strdup before using.

Correcting these two problems and keeping itoa would look like this:

int main() {
    char ent[3] = {'E','0'};
    char a[2];
    int n=5;//exemplo

    for (int i = 0; i < n; i++) {
        itoa(i,a,10);
        char *ent_dup = strdup(ent); //criar duplicado
        strcat(ent_dup,a); //usar duplicado até ao fim
        printf("novo ent: %s \n ",ent_dup);
        free(ent_dup); //liberar duplicado
    }
    return 0;
}

Note that you have to release the string returned by strdup through free if you want to avoid memory leaks.

Using sprintf would look like this:

int main() {
    char ent[3] = {'E','0'};
    char saida[10];
    int n=5;

    for (int i = 0; i < n; i++) {
        sprintf(saida, "%s%01d", ent, i);
        printf("novo ent: %s \n ",saida);
    }
    return 0;
}

View this latest version on Ideone

    
19.11.2018 / 00:32