In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation, with the goal of enhancing program readability.
For the naming of identifiers, a common recommendation is "Use meaningful identifiers." A single word may not be as meaningful, or specific, as multiple words. Consequently, some naming conventions specify rules for the treatment of "compound" identifiers containing more than one word. Three most popular naming conventions are explained below:
(From Wikipedia, the free encyclopedia, https://en.wikipedia.org/wiki/Naming_convention_(programming))
In this problem, we consider an Integrated Development Environment (IDE) that supports naming convention conversion. We assume that the kebab case is the default naming convention. The IDE can convert to a different naming convention according to a programmer's preference. The IDE can also revert back to the default naming convention for the integration among multiple program files.
We first design the abstract base class ‘Case’ as an interface, which contains two pure virtual member functions:
#include <iostream>
#include <string>
using namespace std;
class Case{
protected:
bool converted;
string name;
public:
virtual void convert() = 0;
virtual void revert() = 0;
virtual ~Case(){}
Case(string s): converted(false), name(s){}
void show(ostream &os){
os << name;
}
bool is_converted() const{
return converted;
}
};
class SnakeCase : public Case{
public:
SnakeCase(string s): Case(s){}
void convert(){
converted = true;
for(int i = 0; i < name.length(); i++){
if(name[i] == '-') name[i] = '_';
}
}
void revert(){
converted = false;
for(int i = 0; i < name.length(); i++){
if(name[i] == '_') name[i] = '-';
}
}
};
class CamelCase : public Case{
public:
CamelCase(string s): Case(s){}
void convert();
void revert();
};
In ‘function.h’, we also add the class ‘SnakeCase’ as a sample of implementing a derived class of the base class ‘Case’.
You are asked to implement another derived class ‘CamelCase’ by overriding the two virtual functions, convert and revert.
Note
It is highly recommended to practice std::stringstream in this problem. Here is a simple code that shows you how to use stringstream:
#include <iostream>
#include <sstream> // header file for stringstream
using namespace std;
int main(){
string s;
stringstream ss;
getline(cin, s);
ss << s;
while(!ss.eof()){
string t;
getline(ss, t, ' ');
cout << t << "\n";
}
return 0;
}
/*
- Sample Input
This is a sentence for testing this code.
- Sample Output
This
is
a
sentence
for
testing
this
code.
*/
For more information, please refer to this article.
A line contains only lowercase letters and hyphens.
The Snake case and the Camel case for the input string. Please refer to the sample output for details.