How to Append a Character to a String in C
Given a string str and a character ch, this article tells about how to append this character ch to this string str at the end.
Examples:
Input: str = "Geek", ch = 's' Output: "Geeks" Input: str = "skee", ch = 'G' Output: "skeeG"
Approach:
- Get the string str and character ch
- 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.
Syntax:
char *strncat(char *dest, const char *src, size_t n)
Parameters: This method accepts the following parameters:
- dest: the string where we want to append.
- src: the string from which ‘n’ characters are going to append.
- n: represents the maximum number of character to be appended. size_t is an unsigned integral type.
- Print or return the appended string str.
Below is the implementation of the above approach:
// C program to Append a Character to a String #include <stdio.h> #include <string.h> int main() { // declare and initialize string char str[] = "Geek"; // declare and initialize char char ch = 's'; // print string printf("Original String: %s\n", str); printf("Character to be appended: %c\n", ch); // append ch to str strncat(str, &ch;, 1); // print string printf("Appended String: %s\n", str); return 0; } |
chevron_right
filter_none
Output:
Original String: Geek Character to be appended: s Appended String: Geeks
Recommended Posts:
- std::string::append vs std::string::push_back() vs Operator += in C++
- Replace every character of string by character whose ASCII value is K times more than it
- Find a string such that every character is lexicographically greater than its immediate next character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Converting one string to other using append and delete last operations
- Form N-copy string with add, remove and append operations
- Map every character of one string to another such that all occurrences are mapped to the same character
- Append a digit in the end to make the number equal to the length of the remaining string
- Replace every character of a string by a different character
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find the character in first string that is present at minimum index in second string
- Convert the string into palindrome string by changing only one character.
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



