Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Friday Programming Quiz [modified]

Friday Programming Quiz [modified]

Scheduled Pinned Locked Moved The Lounge
delphihtmldatabasedebuggingxml
44 Posts 17 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rama Krishna Vavilala

    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 -> ESOP

    In 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

    N Offline
    N Offline
    Nish Nishant
    wrote on last edited by
    #5

    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*)

    R 1 Reply Last reply
    0
    • R Rama Krishna Vavilala

      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 -> ESOP

      In 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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #6

      Rama Krishna Vavilala wrote:

      Column names in a database are named using Pascal casing

      You create multiple language resources and a map table: ColumnNameMap ID ColumnName ResourceID The remainder is obvious.

      led mike

      1 Reply Last reply
      0
      • R Rama Krishna Vavilala

        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 -> ESOP

        In 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

        S Offline
        S Offline
        Shog9 0
        wrote on last edited by
        #7

        Now, with extra dirt!

        function DisplayNameFromColumnName(colName)
        {
        return colName.match(/([A-Z](?:[A-Z]*(?=[A-Z]|$)|[^A-Z]+))/g).join(' ');
        }

        R 1 Reply Last reply
        0
        • N Nish Nishant

          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*)

          R Offline
          R Offline
          Rama Krishna Vavilala
          wrote on last edited by
          #8

          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

          N 2 Replies Last reply
          0
          • R Rama Krishna Vavilala

            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 -> ESOP

            In 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

            A Offline
            A Offline
            Alvaro Mendez
            wrote on last edited by
            #9

            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

            1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              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

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #10

              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*)

              1 Reply Last reply
              0
              • S Shog9 0

                Now, with extra dirt!

                function DisplayNameFromColumnName(colName)
                {
                return colName.match(/([A-Z](?:[A-Z]*(?=[A-Z]|$)|[^A-Z]+))/g).join(' ');
                }

                R Offline
                R Offline
                Rama Krishna Vavilala
                wrote on last edited by
                #11

                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

                S 1 Reply Last reply
                0
                • C Christian Graus

                  StringBuilder sb = new StringBuilder(); for(int i = 0;i

                  R Offline
                  R Offline
                  Rama Krishna Vavilala
                  wrote on last edited by
                  #12

                  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

                  P 1 Reply Last reply
                  0
                  • R Rama Krishna Vavilala

                    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

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #13

                    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*)

                    R 1 Reply Last reply
                    0
                    • N Nish Nishant

                      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*)

                      R Offline
                      R Offline
                      Rama Krishna Vavilala
                      wrote on last edited by
                      #14

                      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

                      N 1 Reply Last reply
                      0
                      • R Rama Krishna Vavilala

                        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 -> ESOP

                        In 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

                        Steve EcholsS Offline
                        Steve EcholsS Offline
                        Steve Echols
                        wrote on last edited by
                        #15

                        Seeing all these solutions reminds me I really need to learn regex. :^)


                        - S 50 cups of coffee and you know it's on!

                        • S
                          50 cups of coffee and you know it's on!
                          Code, follow, or get out of the way.
                        S 1 Reply Last reply
                        0
                        • R Rama Krishna Vavilala

                          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 -> ESOP

                          In 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

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #16

                          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?

                          R 1 Reply Last reply
                          0
                          • R Rama Krishna Vavilala

                            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

                            S Offline
                            S Offline
                            Shog9 0
                            wrote on last edited by
                            #17

                            I wrote it that way at first, then realized it was leaving an extra space on the end, then got bored and switched to join(). I like join(). I've seen it poorly-implemented too often not to love a library implementation. ;)

                            1 Reply Last reply
                            0
                            • C Christian Graus

                              StringBuilder sb = new StringBuilder(); for(int i = 0;i

                              S Offline
                              S Offline
                              Shog9 0
                              wrote on last edited by
                              #18

                              Christian Graus wrote:

                              IUseHTMLALot

                              I hate those. It may be incorrect, but i'll still write it as IUseHtmlALot.

                              1 1 Reply Last reply
                              0
                              • R Rama Krishna Vavilala

                                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

                                N Offline
                                N Offline
                                Nish Nishant
                                wrote on last edited by
                                #19

                                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*)

                                1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  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?

                                  R Offline
                                  R Offline
                                  Rama Krishna Vavilala
                                  wrote on last edited by
                                  #20

                                  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

                                  M 1 Reply Last reply
                                  0
                                  • R Rama Krishna Vavilala

                                    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 -> ESOP

                                    In 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 Offline
                                    C Offline
                                    Chris Losinger
                                    wrote on last edited by
                                    #21

                                    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;
                                    

                                    }

                                    image processing | batch image processing | blogging

                                    1 Reply Last reply
                                    0
                                    • C Christian Graus

                                      StringBuilder sb = new StringBuilder(); for(int i = 0;i

                                      R Offline
                                      R Offline
                                      Rama Krishna Vavilala
                                      wrote on last edited by
                                      #22

                                      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

                                      P 1 Reply Last reply
                                      0
                                      • K Kacee Giger

                                        Does this handle the BodyHTML -> Body HTML case--I believe your solution would give "Body H T M L"?

                                        M Offline
                                        M Offline
                                        Matt Gerrans
                                        wrote on last edited by
                                        #23

                                        Well, all we need to do is just compile his solution with the Plain English compiler and try it out! Grande?

                                        Matt Gerrans

                                        D 1 Reply Last reply
                                        0
                                        • R Rama Krishna Vavilala

                                          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 -> ESOP

                                          In 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

                                          S Offline
                                          S Offline
                                          Shog9 0
                                          wrote on last edited by
                                          #24

                                          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;
                                          }

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups