简体   繁体   中英

How can I concatenate characters to a string in C?

It's an exercise where I have to build a function which returns a string called "secret identity" composed with your birth date, your name and your mother's name (for example, if "02/12/2007", "LUCY TOLKIEN" and "JENNIFER" it returns "20070212LT*J") but I'm struggling to concatenate the characters (like "L" and "T" of "LUCY TOLKIEN") to the string called "secret identity". I hope I could explain it well. There's what I did by far:

int length(char * s) {
    int i, n = 0;
    for (i = 0; *(s + i) != '\0'; i++) {
        n++;
    }

    return n;
}

void concatenate(char * s, char * t) {
    int i = 0;
    int j;

    while (*(s+i) != '\0') {
        i++;
    }

    for (j = 0; *(t+i) != '\0'; j++) {
        *(s + i) = *(t + j);
        i++;
    }
    *(s + i + 1) = '\0';
}

void copy(char * dest, char * orig) {
    int i;
    for (i = 0; *(orig + i) != '\0'; i++) {
        *(dest + i) = *(orig + i);
    }

    *(dest + i) = '\0';
}

void geraIdentidade(void) {
    char * ident;
    int lname, ldate, lmom;

    char name[80];
    printf("Name: ");
    scanf(" %[^\n]s", name);

    lname = length(name);

    char date[11];
    printf("Date: ");
    scanf(" %[^\n]s", date);

    ldate = length(date);

    char mom[20];
    printf("Name (mom): ");
    scanf(" %[^\n]s", mom);

    lmom = length(mom);

    char day[3], month[3], year[5];
    int i, j, k; 

    for (i = 0; date[i] != '/'; i++) {
        day[i] = date[i];
    day[i + 1] = '\0';
    }

    for (j = 3, i = 0; date[j] != '/'; j++, i++) {
        month[i] = date[j];
    month[i + 1] = '\0';
    }

    for (k = 6, i = 0; k <= 9; k++, i++) {
        year[i] = date[k];
    year[i + 1] = '\0';
    }

    ident = (char*)malloc((lmom + ldate + lname) * sizeof(char)); //change lenght

    if (ident != NULL) {
        copy(ident, year);
        concatenate(ident, month);
        concatenate(ident, day);
    }
    else {
        return NULL;
    }

    printf("%s\n", ident);


}

int main(void) {

    geraIdentidade();

    return 0;
}

For my opinion, 3 functions in your code:

int length(char * s)
void concatenate(char * s, char * t)
void copy(char * dest, char * orig)

You can make the code easier to do when you use some C standard functions in <string.h> :

size_t strlen(const char *s); // for length
char *strcpy(char *dest, const char *src); // for copy 
char *strcat(char *dest, const char *src); // for concatenation

When you want to concatenate string and character , you just need to convert character to string by adding the \0 character to the character you want to concatenate. For example, if you want to concatenate T to string 20070212L :

#include <stdio.h>
#include <string.h>

int main()
{
    char str[11] = "20070212L";
    char ch[2] = "\0";
    ch[0] = 'T';
    strcat(str, ch);
    printf("str = %s", str);
    return 0;
}

The output:

str = 20070212LT 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM