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. General Programming
  3. C / C++ / MFC
  4. Need an Algorithm

Need an Algorithm

Scheduled Pinned Locked Moved C / C++ / MFC
databasealgorithmshelptutorialquestion
13 Posts 8 Posters 0 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.
  • P P Rex

    I´am working on a project where i have to query some values from a database. I have to convert those values into letters from 'a' to 'z'. example: value: letter 1 -> 'a' 2 -> 'b' 3 -> 'c' ... 27 -> 'aa' 28 -> 'ab' 29 -> 'ac' The letters can go up to 'zzzz' Can anybody help me with this stupid thing? P.

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #2

    Maybe something like that:

    int Number = 57; // For example
    char szString[255];
    strcpy(szString,"");

    while (true)
    {
    if (Number>=27)
    {
    strcat(szString,"a");
    Number -= 26;
    }
    else
    {
    char Temp;
    Temp = Number + 97; // 97 is the ASCII code for 'a'
    int StrLen = strlen(szString);
    szString[StrLen] = Temp;
    szString[StrLen+1] = '\0';
    break; //end while loop
    }
    }

    Ok, I didn't tested it so there could be some mistakes but this gives you the general idea

    T 1 Reply Last reply
    0
    • C Cedric Moonen

      Maybe something like that:

      int Number = 57; // For example
      char szString[255];
      strcpy(szString,"");

      while (true)
      {
      if (Number>=27)
      {
      strcat(szString,"a");
      Number -= 26;
      }
      else
      {
      char Temp;
      Temp = Number + 97; // 97 is the ASCII code for 'a'
      int StrLen = strlen(szString);
      szString[StrLen] = Temp;
      szString[StrLen+1] = '\0';
      break; //end while loop
      }
      }

      Ok, I didn't tested it so there could be some mistakes but this gives you the general idea

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #3

      you code won't work because you consider that if (Number>=27) only... but if Number == 53, the result must be "ba", etc... as P-Rex said, there are until "zzzz" possibilities, that means 26^4 = 456976 cases...


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      C 1 Reply Last reply
      0
      • P P Rex

        I´am working on a project where i have to query some values from a database. I have to convert those values into letters from 'a' to 'z'. example: value: letter 1 -> 'a' 2 -> 'b' 3 -> 'c' ... 27 -> 'aa' 28 -> 'ab' 29 -> 'ac' The letters can go up to 'zzzz' Can anybody help me with this stupid thing? P.

        A Offline
        A Offline
        Andrew Kirillov
        wrote on last edited by
        #4

        CString str; int number = 53; do { number--; str.Insert(0, 'a' + (number % 26)); number /= 26; } while (number > 0); TRACE(_T("str: %s"), str);

        P 1 Reply Last reply
        0
        • P P Rex

          I´am working on a project where i have to query some values from a database. I have to convert those values into letters from 'a' to 'z'. example: value: letter 1 -> 'a' 2 -> 'b' 3 -> 'c' ... 27 -> 'aa' 28 -> 'ab' 29 -> 'ac' The letters can go up to 'zzzz' Can anybody help me with this stupid thing? P.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #5

          int iVal = 453; // For example
          char Result[5] = "";

          if (iVal >= 17577) { // For the most left character
          Result[0] = (iVal % 26) - 1 + 'A';
          }
          else {
          Result[0] = '\0';
          }

          if (iVal >= 677) { // For the second left character
          Result[1] = (iVal % 26) - 1 + 'A';
          }
          else {
          Result[1] = '\0';
          }

          if (iVal >= 27) { // For the third left character
          Result[2] = (iVal % 26) - 1 + 'A';
          }
          else {
          Result[2] = '\0';
          }

          if (iVal >= 1) { // For the most right character
          Result[3] = (iVal % 26) - 1 + 'A';
          }
          else {
          Result[3] = '\0';
          }

          Result[4] = '\0';

          // Re-order the string (erases the '\0' at the beginning of the string)...
          for (int i = 0, i < 4, i++) {
          if (Result[1] == '\0') {
          Result[i] = Result[i+1];
          }
          }

          i didnot tested however... :cool:


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          B A 2 Replies Last reply
          0
          • T toxcct

            you code won't work because you consider that if (Number>=27) only... but if Number == 53, the result must be "ba", etc... as P-Rex said, there are until "zzzz" possibilities, that means 26^4 = 456976 cases...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #6

            I think you didn't understand completely my code ;) For exemple if Number == 53, the result must be "ab" and not "ba". Thus in my code, if (Number >= 27), I DON'T jump out of the loop but comes once again in the loop, thus adding 'b' to the already existing string (that contains 'a'). Is it a little bit clearer ? [EDIT] Sorry, I don't add 'b' but 'z' because 53-27 = 26 that corresponds to 'z' :) [EDIT2] After 2 minutes of reflexion, I see that there effectively a problem in the code :~ . There will only be 'a' in the begining of the string... I must not subtract but rather divide by 26... But I think the other solutions are good

            C 1 Reply Last reply
            0
            • A Andrew Kirillov

              CString str; int number = 53; do { number--; str.Insert(0, 'a' + (number % 26)); number /= 26; } while (number > 0); TRACE(_T("str: %s"), str);

              P Offline
              P Offline
              P Rex
              wrote on last edited by
              #7

              Thanks, you are my hero. I didn´t thought that this is so easy :-D Pascal

              T 1 Reply Last reply
              0
              • P P Rex

                Thanks, you are my hero. I didn´t thought that this is so easy :-D Pascal

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #8

                As he properly realized, going from a number to a 'A' to 'ZZZZ' representation is the same as printing the number base 26 where the digits are the letters 'A' - 'Z'. If you wanted to write a number converter to print that value as decimal, then just change the / 26 to / 10 and the initial character from 'A' to '0'. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                1 Reply Last reply
                0
                • P P Rex

                  I´am working on a project where i have to query some values from a database. I have to convert those values into letters from 'a' to 'z'. example: value: letter 1 -> 'a' 2 -> 'b' 3 -> 'c' ... 27 -> 'aa' 28 -> 'ab' 29 -> 'ac' The letters can go up to 'zzzz' Can anybody help me with this stupid thing? P.

                  B Offline
                  B Offline
                  Blake Miller
                  wrote on last edited by
                  #9

                  What value is represented by '0' ? If you know that, then you have a base 26 system. See, everything goes up to 26, then it carries over one place. You are just mapping a base 26 numeric system to letters.

                  1 Reply Last reply
                  0
                  • T toxcct

                    int iVal = 453; // For example
                    char Result[5] = "";

                    if (iVal >= 17577) { // For the most left character
                    Result[0] = (iVal % 26) - 1 + 'A';
                    }
                    else {
                    Result[0] = '\0';
                    }

                    if (iVal >= 677) { // For the second left character
                    Result[1] = (iVal % 26) - 1 + 'A';
                    }
                    else {
                    Result[1] = '\0';
                    }

                    if (iVal >= 27) { // For the third left character
                    Result[2] = (iVal % 26) - 1 + 'A';
                    }
                    else {
                    Result[2] = '\0';
                    }

                    if (iVal >= 1) { // For the most right character
                    Result[3] = (iVal % 26) - 1 + 'A';
                    }
                    else {
                    Result[3] = '\0';
                    }

                    Result[4] = '\0';

                    // Re-order the string (erases the '\0' at the beginning of the string)...
                    for (int i = 0, i < 4, i++) {
                    if (Result[1] == '\0') {
                    Result[i] = Result[i+1];
                    }
                    }

                    i didnot tested however... :cool:


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc]

                    B Offline
                    B Offline
                    Blake Miller
                    wrote on last edited by
                    #10

                    :wtf:

                    T 1 Reply Last reply
                    0
                    • B Blake Miller

                      :wtf:

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #11

                      Blake Miller wrote: :wtf: what ?:confused:


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc]

                      1 Reply Last reply
                      0
                      • C Cedric Moonen

                        I think you didn't understand completely my code ;) For exemple if Number == 53, the result must be "ab" and not "ba". Thus in my code, if (Number >= 27), I DON'T jump out of the loop but comes once again in the loop, thus adding 'b' to the already existing string (that contains 'a'). Is it a little bit clearer ? [EDIT] Sorry, I don't add 'b' but 'z' because 53-27 = 26 that corresponds to 'z' :) [EDIT2] After 2 minutes of reflexion, I see that there effectively a problem in the code :~ . There will only be 'a' in the begining of the string... I must not subtract but rather divide by 26... But I think the other solutions are good

                        C Offline
                        C Offline
                        camoguard
                        wrote on last edited by
                        #12

                        You sound like you have it. But in case you haven't thought of this, you can imagine you are working with a base 27 system of numbers and you are converting from decimal. Begin by checking if your value is greater than (27^3+1) if it is get the mod of your input number by (27^3+1 because a is 1), that's your first digit which falls between 0 and 26. then subtract that number off your working total. Then check if the remainder is bigger than (27^2+1). If it is, then mod by (27^2+1), that's your second digit. And so on. Since you are working with a-z just add the value of the char('a') to each mod and save yourself a switch statement. One for loop of four iterations should do it.

                        1 Reply Last reply
                        0
                        • T toxcct

                          int iVal = 453; // For example
                          char Result[5] = "";

                          if (iVal >= 17577) { // For the most left character
                          Result[0] = (iVal % 26) - 1 + 'A';
                          }
                          else {
                          Result[0] = '\0';
                          }

                          if (iVal >= 677) { // For the second left character
                          Result[1] = (iVal % 26) - 1 + 'A';
                          }
                          else {
                          Result[1] = '\0';
                          }

                          if (iVal >= 27) { // For the third left character
                          Result[2] = (iVal % 26) - 1 + 'A';
                          }
                          else {
                          Result[2] = '\0';
                          }

                          if (iVal >= 1) { // For the most right character
                          Result[3] = (iVal % 26) - 1 + 'A';
                          }
                          else {
                          Result[3] = '\0';
                          }

                          Result[4] = '\0';

                          // Re-order the string (erases the '\0' at the beginning of the string)...
                          for (int i = 0, i < 4, i++) {
                          if (Result[1] == '\0') {
                          Result[i] = Result[i+1];
                          }
                          }

                          i didnot tested however... :cool:


                          TOXCCT >>> GEII power
                          [toxcct][VisualCalc]

                          A Offline
                          A Offline
                          Alton Williams
                          wrote on last edited by
                          #13

                          Dosen't work toxcct

                          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