Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append Char To String in C?

Tags:

c

string

How do I append a single char to a string in C?

i.e

char* str = "blablabla"; char c = 'H'; str_append(str,c); /* blablablaH */ 
like image 272
ApprenticeHacker Avatar asked Apr 23 '12 11:04

ApprenticeHacker


People also ask

Can you append a char to a string?

Use the strncat() function to append the character ch at the end of str. strncat() is a predefined function used for string handling. string. h is the header file required for string functions.

How do I append a char to a string in C++?

The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.

How do you append to a string?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

Can you concatenate strings in C?

In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.


1 Answers

char* str = "blablabla";      

You should not modify this string at all. It resides in implementation defined read only region. Modifying it causes Undefined Behavior.

You need a char array not a string literal.

Good Read:
What is the difference between char a[] = "string"; and char *p = "string";

like image 156
Alok Save Avatar answered Sep 26 '22 14:09

Alok Save