Friday Programming Quiz [modified]
-
C# version :
class Program
{
static string Func(string str)
{
str = new Regex("[A-Z][a-z]").Replace(str,
new MatchEvaluator(delegate(Match m)
{ return m.Value.Insert(0, " "); }));
return new Regex("[a-z][A-Z]").Replace(str,
new MatchEvaluator(delegate(Match m)
{ return m.Value.Insert(1, " "); }));
}
static void Main(string[] args)
{
foreach (string s in arr)
Console.WriteLine("{0} -> {1}", s, Func(s));
}static string\[\] arr = new string\[\] {"BodyHTML", "LastAccessedTime", "XMLValue", "ESOP"};
}
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*)Right direction but can be further simplified:).
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
I recently encountered/solved this problem and it is fairly simple. Column names in a database are named using Pascal casing, however to display it in a user friendly manner words need to be separated with spaces to generate display names. Following examples show the output for some strings.
Name Display Name
BodyHTML -> Body HTML
LastAccessedTime -> Last Accessed Time
ESOP -> ESOPIn a language of your choice implement a procedure that will convert the column names to display names.
String DisplayNameFromColumnName(String columnName) {
}-- modified at 16:56 Friday 1st December, 2006 Removed XMLValue -> XML Value
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
One implementation, in C#:
string DisplayNameFromColumnName(string value)
{
return Regex.Replace(Regex.Replace(value, "([a-z])([A-Z])", "$1 $2"), "([A-Z])([A-Z][a-z])", "$1 $2");
}Regards, Alvaro
A casual stroll through the lunatic asylum shows that faith does not prove anything. - Friedrich Nietzsche
-
Right direction but can be further simplified:).
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Rama Krishna Vavilala wrote:
Right direction but can be further simplified.
I forgot about the capture syntax in C# - so didn't use them :-)
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
Now, with extra dirt!
function DisplayNameFromColumnName(colName)
{
return colName.match(/([A-Z](?:[A-Z]*(?=[A-Z]|$)|[^A-Z]+))/g).join(' ');
}I prefer String.replace to join.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
StringBuilder sb = new StringBuilder(); for(int i = 0;i
Christian Graus wrote:
IUseHTMLALot
Yes! But this is a fun Quiz ignore those issues.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
Right direction but can be further simplified:).
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Rama Krishna Vavilala wrote:
Right direction but can be further simplified.
Simpliefied version :-
static string Func(string str)
{
return new Regex("([a-z])([A-Z])").Replace(
new Regex("([A-Z])([a-z])").Replace(str, " $1$2"), "$1 $2");
}Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
Rama Krishna Vavilala wrote:
Right direction but can be further simplified.
Simpliefied version :-
static string Func(string str)
{
return new Regex("([a-z])([A-Z])").Replace(
new Regex("([A-Z])([a-z])").Replace(str, " $1$2"), "$1 $2");
}Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*)What about something like "Name";)
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
I recently encountered/solved this problem and it is fairly simple. Column names in a database are named using Pascal casing, however to display it in a user friendly manner words need to be separated with spaces to generate display names. Following examples show the output for some strings.
Name Display Name
BodyHTML -> Body HTML
LastAccessedTime -> Last Accessed Time
ESOP -> ESOPIn a language of your choice implement a procedure that will convert the column names to display names.
String DisplayNameFromColumnName(String columnName) {
}-- modified at 16:56 Friday 1st December, 2006 Removed XMLValue -> XML Value
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Seeing all these solutions reminds me I really need to learn regex. :^)
- S 50 cups of coffee and you know it's on!
-
I prefer String.replace to join.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
I recently encountered/solved this problem and it is fairly simple. Column names in a database are named using Pascal casing, however to display it in a user friendly manner words need to be separated with spaces to generate display names. Following examples show the output for some strings.
Name Display Name
BodyHTML -> Body HTML
LastAccessedTime -> Last Accessed Time
ESOP -> ESOPIn a language of your choice implement a procedure that will convert the column names to display names.
String DisplayNameFromColumnName(String columnName) {
}-- modified at 16:56 Friday 1st December, 2006 Removed XMLValue -> XML Value
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
I can put SPACEs in my column names; it's not a good idea, but I can. I suppose I can get the description from the metadata and use that if it's not empty. And I just don't think there's a 100% fool-proof way of doing the task, so why bother?
-
StringBuilder sb = new StringBuilder(); for(int i = 0;i
-
What about something like "Name";)
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Rama Krishna Vavilala wrote:
What about something like "Name"
Blast! It adds a space to the beginning. Oh well, a call to
Trim()
should fix that.Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
I can put SPACEs in my column names; it's not a good idea, but I can. I suppose I can get the description from the metadata and use that if it's not empty. And I just don't think there's a 100% fool-proof way of doing the task, so why bother?
I agree that there is no 100% fool proof way esp. for cases like IUseHTMLALot or XMLValue. But this is for fun.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
I recently encountered/solved this problem and it is fairly simple. Column names in a database are named using Pascal casing, however to display it in a user friendly manner words need to be separated with spaces to generate display names. Following examples show the output for some strings.
Name Display Name
BodyHTML -> Body HTML
LastAccessedTime -> Last Accessed Time
ESOP -> ESOPIn a language of your choice implement a procedure that will convert the column names to display names.
String DisplayNameFromColumnName(String columnName) {
}-- modified at 16:56 Friday 1st December, 2006 Removed XMLValue -> XML Value
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
as long as you're taking out the CAPSLower case
string DisplayNameFromColumnName(const char*l)
{
string O = "";while (\*l) { O += \*l; O += (islower(\*l) && isupper(\*(l+1))) ? " " : ""; l++; } return O;
}
-
StringBuilder sb = new StringBuilder(); for(int i = 0;i
BTW: Probably that is why the .NET naming guidelines state that any acronym > 2 letters should not be all capitalized.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
Does this handle the BodyHTML -> Body HTML case--I believe your solution would give "Body H T M L"?
Well, all we need to do is just compile his solution with the Plain English compiler and try it out! Grande?
Matt Gerrans
-
I recently encountered/solved this problem and it is fairly simple. Column names in a database are named using Pascal casing, however to display it in a user friendly manner words need to be separated with spaces to generate display names. Following examples show the output for some strings.
Name Display Name
BodyHTML -> Body HTML
LastAccessedTime -> Last Accessed Time
ESOP -> ESOPIn a language of your choice implement a procedure that will convert the column names to display names.
String DisplayNameFromColumnName(String columnName) {
}-- modified at 16:56 Friday 1st December, 2006 Removed XMLValue -> XML Value
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Don't feel like firing up the compiler, but felt like being old-school...
/*
* colName points to column name, cannot be NULL
* output points to buffer suitable for holding display name, cannot be NULL
* maxOutputLen is the maximum number of characters that can be stored in output,
* excluding the inevitable NULL terminator. output must be maxOutputLen+1 chars in length
*/
char* DisplayNameFromColumnName(const char* colName, char* output, int maxOutputLen)
{
int inPos = 0;
int outPos = 0;
while ( colName[inPos] && outPos < maxOutputLen)
{
if ( isupper(colName[inPos]) && colName[inPos+1] && !isupper(colName[inPos+1]) )
{
output[outPos++] = ' ';
if ( outPos == maxOutputLen )
break;
}
output[outPos++] = colName[inPos++];
}
output[outPos] = '\0';return output;
} -
I agree that there is no 100% fool proof way esp. for cases like IUseHTMLALot or XMLValue. But this is for fun.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
IUseHTMLALot is problematic, but XMLValue seems okay. If you have any sequence of caps followed by lowercase, then you break before the last cap, right?
Matt Gerrans
-
Christian Graus wrote:
IUseHTMLALot
Yes! But this is a fun Quiz ignore those issues.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
I can't. If I do a thing I want to do it right (or at least handle all the known problems). How about fields "PriceAtCompUSA" and "IsOwnedByPaulMcCartney" Plus, breaking the field names will make it difficult to parse the resultant file. It's just not worth the effort. Well, unless I'm getting paid.
-
Seeing all these solutions reminds me I really need to learn regex. :^)
- S 50 cups of coffee and you know it's on!