How to add characters to the end of a string in the C language?

In C language, you can use the string concatenation function strcat() to add characters to the end of a string. Here is an example code:

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

int main() {
    char str[] = "Hello";
    char ch = '!';
    
    strcat(str, &ch);
    
    printf("%s\n", str); // 输出:Hello!
    
    return 0;
}

In the code above, we have defined a string named str and initialized it to “Hello”. Next, we defined a character ch and assigned it the value ‘!’. We then used the strcat() function to append the character ch to the end of the string str. Finally, we used the printf() function to display the result.

It is important to note that the second parameter of the strcat() function must be a pointer type, so we use &ch to get the address of the character ch.

bannerAds