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

    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
          • R Rama Krishna Vavilala

            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 Offline
            M Offline
            Matt Gerrans
            wrote on last edited by
            #25

            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

            1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              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 Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #26

              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.

              1 Reply Last reply
              0
              • Steve EcholsS Steve Echols

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


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

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

                Eh, it's just another hammer. And the truth of it is, code with too many regexps in it can be nearly unmaintainable. Great for code that won't last long or that should be replaced rather than tweaked... not so good for code intended to grow and mature.

                Steve EcholsS 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
                  #28

                  Rama Krishna Vavilala wrote:

                  Removed XMLValue -> XML Value

                  Bah! I could've been the second respondent if it weren't for that! Now i have to settle for 5th, 10th, 11th...

                  1 Reply Last reply
                  0
                  • S Shog9 0

                    Christian Graus wrote:

                    IUseHTMLALot

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

                    1 Offline
                    1 Offline
                    123 0
                    wrote on last edited by
                    #29

                    Shog9 wrote:

                    i'll still write it as IUseHtmlALot.

                    I use HTML very little. Is that a problem?

                    1 Reply Last reply
                    0
                    • S Shog9 0

                      Eh, it's just another hammer. And the truth of it is, code with too many regexps in it can be nearly unmaintainable. Great for code that won't last long or that should be replaced rather than tweaked... not so good for code intended to grow and mature.

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

                      Yes! Now I can take the weekend off! :) I guess it would be good to know it, in case I ever see it out in the wild though. Or, I guess I could just look it up when I need it.


                      - 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.
                      P 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

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #31

                        return lookupUserFriendlyName[columnName];

                        ;P Marc

                        Thyme In The Country

                        People are just notoriously impossible. --DavidCrow
                        There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
                        People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

                        P 1 Reply Last reply
                        0
                        • R Rama Krishna Vavilala

                          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 Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #32

                          Microsoft doesn't get to decide on the proper capitalization of the technologies created by others.

                          1 Reply Last reply
                          0
                          • Steve EcholsS Steve Echols

                            Yes! Now I can take the weekend off! :) I guess it would be good to know it, in case I ever see it out in the wild though. Or, I guess I could just look it up when I need it.


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

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

                            Too many dialects.

                            1 Reply Last reply
                            0
                            • M Marc Clifton

                              return lookupUserFriendlyName[columnName];

                              ;P Marc

                              Thyme In The Country

                              People are just notoriously impossible. --DavidCrow
                              There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
                              People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

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

                              DoWhatImThinking ( Data )

                              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"?

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #35

                                Nope. It should only do it if the previous character was lower case. But, then again, I wrote the "psuedo" in my head after looking at the problem for all of 15 seconds with nothing more than the CP post window in front of me.

                                Dave Kreskowiak Microsoft MVP - Visual Basic

                                M 1 Reply Last reply
                                0
                                • M Matt Gerrans

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

                                  Matt Gerrans

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #36

                                  :laugh:!

                                  Dave Kreskowiak Microsoft MVP - Visual Basic

                                  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

                                    T Offline
                                    T Offline
                                    Tomas Petricek
                                    wrote on last edited by
                                    #37

                                    My F# solution is following :->

                                    open Array;;
                                    open System;;
                                    open System.Text;;

                                    let DisplayNameFromColumnName (str:string) =
                                    let l = str.Length in
                                    let nb = fun n -> ( (if (n = 0) then 'x' else str.[n-1]),
                                    (str.[n]), (if (n = l-1) then 'X' else str.[n+1]) ) in
                                    let sb = new StringBuilder() in
                                    let ap (c:char) = ignore(sb.Append(c)) in
                                    let up c = Char.IsUpper(c) in
                                    let lo c = Char.IsLower(c) in
                                    str.ToCharArray() |> iteri ( fun n _ -> let (p,c,n) = (nb n) in
                                    if ((lo(p) && up(c)) || (up(c) && lo(n))) then ap(' '); ap(c) );
                                    (sb.ToString()).Trim();;

                                    If works on the "XMLValue" example too...

                                    Tomas Petricek, C# MVP
                                    Tomasp.net | My Photos | My Blog (C# 3, LINQ, F# etc..)

                                    N 1 Reply Last reply
                                    0
                                    • T Tomas Petricek

                                      My F# solution is following :->

                                      open Array;;
                                      open System;;
                                      open System.Text;;

                                      let DisplayNameFromColumnName (str:string) =
                                      let l = str.Length in
                                      let nb = fun n -> ( (if (n = 0) then 'x' else str.[n-1]),
                                      (str.[n]), (if (n = l-1) then 'X' else str.[n+1]) ) in
                                      let sb = new StringBuilder() in
                                      let ap (c:char) = ignore(sb.Append(c)) in
                                      let up c = Char.IsUpper(c) in
                                      let lo c = Char.IsLower(c) in
                                      str.ToCharArray() |> iteri ( fun n _ -> let (p,c,n) = (nb n) in
                                      if ((lo(p) && up(c)) || (up(c) && lo(n))) then ap(' '); ap(c) );
                                      (sb.ToString()).Trim();;

                                      If works on the "XMLValue" example too...

                                      Tomas Petricek, C# MVP
                                      Tomasp.net | My Photos | My Blog (C# 3, LINQ, F# etc..)

                                      N Offline
                                      N Offline
                                      Nemanja Trifunovic
                                      wrote on last edited by
                                      #38

                                      Tomas Petricek wrote:

                                      My F# solution

                                      That's OCaml, right? Can't you use pattern matching?


                                      Programming Blog utf8-cpp

                                      T S 2 Replies Last reply
                                      0
                                      • N Nemanja Trifunovic

                                        Tomas Petricek wrote:

                                        My F# solution

                                        That's OCaml, right? Can't you use pattern matching?


                                        Programming Blog utf8-cpp

                                        T Offline
                                        T Offline
                                        Tomas Petricek
                                        wrote on last edited by
                                        #39

                                        Yeah, F# is based on OCaml :). As I'm thinking about the problem it could be possible to use another very interesting F# feature called active patterns[^], but I have not played with this feature very much and I'm to lazy to think about it now.. it's friday :-O

                                        Tomas Petricek, C# MVP
                                        Tomasp.net | My Photos | My Blog (C# 3, LINQ, F# etc..)

                                        S 1 Reply Last reply
                                        0
                                        • D Dave Kreskowiak

                                          Nope. It should only do it if the previous character was lower case. But, then again, I wrote the "psuedo" in my head after looking at the problem for all of 15 seconds with nothing more than the CP post window in front of me.

                                          Dave Kreskowiak Microsoft MVP - Visual Basic

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

                                          Dave Kreskowiak wrote:

                                          But, then again, I wrote the "psuedo" in my head after looking at the problem for all of 15 seconds with nothing more than the CP post window in front of me.

                                          Ship it!

                                          Matt Gerrans

                                          D 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