Recap: Basic String Functions
Part of the Logic & Flow section of Coddy's C journey — lesson 19 of 63.
Challenge
EasyWrite a C program that processes pairs of words and demonstrates all the essential string functions you've learned. Your program should:
- Read an integer
nrepresenting the number of word pairs to process - For each word pair:
- Read the first word from input
- Read the second word from input
- Use
strlen()to calculate and print the length of each word - Use
strcmp()to compare the two words and print whether they are identical or different - Create a combined string by concatenating the first word, a space, and the second word using
strcpy()andstrcat() - Print the combined string
- After processing all pairs, find and print the pair that created the longest combined string
Remember to include the <string.h> header to use the string functions.
Your output should display the results in the following format:
Word 1: [word1] (Length: [length1])
Word 2: [word2] (Length: [length2])
Comparison: [identical/different]
Combined: [word1 word2]
Word 1: [word1] (Length: [length1])
Word 2: [word2] (Length: [length2])
Comparison: [identical/different]
Combined: [word1 word2]
...
Longest combined string: [longest_combined_string]For example, if the input is:
3
hello
world
test
test
programming
languageYour output should be:
Word 1: hello (Length: 5)
Word 2: world (Length: 5)
Comparison: different
Combined: hello world
Word 1: test (Length: 4)
Word 2: test (Length: 4)
Comparison: identical
Combined: test test
Word 1: programming (Length: 11)
Word 2: language (Length: 8)
Comparison: different
Combined: programming language
Longest combined string: programming languageThis challenge tests your understanding of combining strlen() for length calculation, strcmp() for string comparison, and strcpy() with strcat() for string concatenation in a comprehensive program that processes multiple string pairs.
Try it yourself
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char word1[100], word2[100];
char combined[200];
char longest_combined[200] = "";
// TODO: Write your code here
// Process each word pair and find the longest combined string
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