Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

8.9 Concatenating Strings

stdio.h

Concatenating or appending strings is not as simple in C as it is in Python. To append one string to the end of a second string, you must use the strcat function.

Prototype
The String Concatenation Function
string.h
char[] strcat(char dest[], char src[]);
The function takes two string arrays (i.e. a character array that contains a string) and appends the src string to the dest string. The destination must be large enough to store the entire resulting string, which is comprised of the original string in dest and the string ing src.

Consider the array declarations

  1. char name[35] = "John";

and suppose we want to append the last name Smith to the name string. This can be done with the function call

  1. strcat(name, "Smith");

which appends the entire string "Smith" to the end of the destination string as illustrated below

You will notice that the null character at the end of the string "John" is replaced with the first character of the source string and a null character is appended at the end of the new string.

Note
Note
It is again important to note that the destination array must be large enough to store the entire resulting string including the null character. No range checking is performed to make sure there is enough room for the entire string and the null character.

The function call above does not leave a blank space between the first and last name. To include the blank space, you must include it as part of the literal string that is being appended or use a second strcat function call:

  1. strcat(name, " ");     // first append a black space to name.
  2. strcat(name, "Smith"); // then append the last name.

which results in

Worked Example
Building a new String

Suppose you are given two string arrays

  1. char firstName[NAME_SIZE];
  2. char lastName[NAME_SIZE];

each of which contains a valid string

and you are tasked with building a new string that contains the individual's full name. How would you complete this task? In Python, it would be accomplished with the simple statement

  1.    fullName = firstName + " " + lastName

In C, this would require the use of a third array (fullName) and three function calls:

  1. char fullName[NAME_SIZE * 2];
  2.  
  3. strcpy(fullName, firstName);
  4. strcat(fullName, " ");
  5. strcat(fullName, lastName);

A complete sample program that solves this task is provided below.

Listing
  1. /* buildname.cc
  2.  *
  3.  * Reads an individual's first and last names and builds and prints their
  4.  * fullname to the terminal.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. const int NAME_SIZE = 35;
  11.  
  12.  
  13. int main()
  14. {
  15.    /* Prompt for and read the first ane last names. */
  16.   char firstName[NAME_SIZE];
  17.   char lastName[NAME_SIZE];
  18.    
  19.   printf("Enter your first name: ");
  20.   scanf("%s", firstName);
  21.  
  22.   printf("Enter your last name: ");
  23.   scanf("%s", lastName);
  24.  
  25.    /* Build the fullname and print it. */
  26.   char fullName[NAME_SIZE * 2];
  27.   strcpy(fullName, firstName);
  28.   strcat(fullName, " ");
  29.   strcat(fullName, lastName);
  30.  
  31.   printf("Your full name is: %s\n", fullName);  
  32. }
Program: buildname.cc
Question 8.9.1

Consider the buildname.py example program above. What would be result if we changed the code segment that builds the full name to the following:

  1. strcat(fullName, firstName);
  2. strcat(fullName, " ");
  3. strcat(fullName, lastName);
Question 8.9.2

Again, consider the buildname.py example program above. Suppose we want to construct the full name in the format "Lastname, Firstname" instead:

Roberts, Sally

What changes would be needed in the code segment that builds the full name?