Question
Develop C++ code 1 Longest prefix You are given an array of strings. You want to code a function to find the longest common prefix
Develop C++ code
1 Longest prefix You are given an array of strings. You want to code a function to find the longest common prefix string amongst this array of strings. If there is no common prefix, return an empty string . Note: the common prefix must be a prefix of all strings. For example, suppose the input array of strings is: [flower,flow,flight]. Then the output of the function is:: fl. As another example, suppose the input is: [dog,racecar,car]. Then the output is: . That is, there is no common prefix among the input strings. More specifically, your task is implementing a function (as shown in the header file: ECLongestPrefix.h): std::string ECLongestPrefix(int numStrings, const std::string *arrayStrings[]);
starter code:
#include "ECLongestPrefix.h"
#include
using namespace std;
// Implement the longest prefix function here...
std::string ECLongestPrefix(int numStrings, const std::string arrayStrings[])
{
// YW: this only serves as a starter code, which just print out the given array of strings
// Replace with your own code here...
for(int i=0; i { cout << arrayStrings[i] << " "; } cout << endl; string strDummy; return strDummy; } Header file #ifndef EC_LONGEST_PREFIX_H #define EC_LONGEST_PREFIX_H #include std::string ECLongestPrefix(int numStrings, const std::string arrayStrings[]); #endif // EC_LONGEST_PREFIX_H
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started