Friday Programming Quiz [modified]
-
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 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
Well, I don't have anynthing in front of me to write this, but here's the thinking: Scan the string one character at a time, copying each character to a new string (or StringBuilder, what have you...) If the current character is a lowercase letter, set a flag and copy the character to the new string ELSE if the lowercase flag is set and the current character is NOT lowercase, insert a space at the end of the string, then copy the current character after it, and reset the flag. Next character... When you get to the end, return the new string.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
Well, I don't have anynthing in front of me to write this, but here's the thinking: Scan the string one character at a time, copying each character to a new string (or StringBuilder, what have you...) If the current character is a lowercase letter, set a flag and copy the character to the new string ELSE if the lowercase flag is set and the current character is NOT lowercase, insert a space at the end of the string, then copy the current character after it, and reset the flag. Next character... When you get to the end, return the new string.
Dave Kreskowiak Microsoft MVP - Visual Basic
Does this handle the BodyHTML -> Body HTML case--I believe your solution would give "Body H T M L"?
-
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
StringBuilder sb = new StringBuilder(); for(int i = 0;i
-
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
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*) -
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 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
-
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 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?
-
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
-
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