Recap: Dynamic String Concat
Part of the Logic & Flow section of Coddy's C journey — lesson 61 of 63.
Challenge
EasyCreate a C program that implements a dynamic string concatenation function to build a text processing utility. Your program should:
- Write a function named
concatenateStringsthat:- Takes two
char*parameters namedstr1andstr2 - Returns a
char*pointing to a dynamically allocated string - Calculates the total memory needed for both strings plus the null terminator
- Uses
malloc()to allocate the exact amount of memory required - Checks if memory allocation was successful (returns NULL if allocation fails)
- Uses
strcpy()to copy the first string into the allocated memory - Uses
strcat()to append the second string to the result - Returns the pointer to the newly created concatenated string
- Takes two
- Write a function named
processTextthat:- Takes three
char*parameters:word1,word2, andseparator - Returns a
char*pointing to a dynamically allocated string - Creates a combined string in the format: word1 + separator + word2
- First calls
concatenateStringsto combineword1andseparator - Then calls
concatenateStringsagain to appendword2to the result - Properly frees any intermediate memory allocations
- Returns the final concatenated result
- Takes three
- In the main function:
- Declare three character arrays of size 50 each:
firstWord,secondWord, andconnector - Read three strings from input representing the first word, second word, and connector
- Call the
processTextfunction with these three strings - Check if the returned pointer is not NULL
- If successful, print the result in this exact format:
Result: [concatenated_string] - If memory allocation failed, print:
Memory allocation failed - Calculate and print the length of the result string in this exact format:
Length: [length] - Free the dynamically allocated memory
- Declare three character arrays of size 50 each:
This challenge tests your mastery of dynamic memory allocation, string manipulation functions, pointer return values, and proper memory management. You'll practice calculating memory requirements, using malloc() and free(), working with multiple string functions, and handling memory allocation failures. The challenge demonstrates how dynamic allocation allows functions to create and return data that persists beyond their scope.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// TODO: Write your concatenateStrings function here
// TODO: Write your processText function here
int main() {
// Read input
char firstWord[50];
char secondWord[50];
char connector[50];
scanf("%s", firstWord);
scanf("%s", secondWord);
scanf("%s", connector);
// TODO: Write your code below
// Call processText function and handle the result
return 0;
}All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions