Menu
Coddy logo textTech

Longest Common Prefix

Lesson 20 of 20 in Coddy's Beginner Challenges - Practice Basic Concepts course.

challenge icon

Challenge

Medium

Write a function named longestCommonPrefix that gets an array of strings and returns the longest common prefix of all the strings in the array.

Examples,

  • longestCommonPrefix(["Hello, Bob", "Hello, Jake"])  ->  "Hello, "
  • longestCommonPrefix(["something", "somehow", "somewhat"])  ->  "some"
  • longestCommonPrefix(["example", "sample"])  ->  ""

Bonus - Make it a recursive function!

Try it yourself

char* longestCommonPrefix(char** a, int aSize) {
    // Write code here
}

All lessons in Beginner Challenges - Practice Basic Concepts