Like Tree4Likes

character converter

This is a discussion on character converter within the C Programming forums, part of the General Programming Boards category; Hello, I would like to know if there is a function which converts a letter to a number.. A to ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    54

    character converter

    Hello,
    I would like to know if there is a function which converts a letter to a number.. A to 1, B to 2 ... Z to 26...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    There will be one after you write it
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    ok.. Thanks!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Or you could use enums if you are interested in a function.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Erm, what have enumerations to do with functions?

    Bye, Andreas

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh Andreas good thing you pointed that out! I wanted to write "if you are not interested in using a function. Thanks.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Good idea! Σε ευχαριστώ!

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    I need a guide, something, for enumerators! thanks!

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well when I need a quick zipped reminder on enums I take a look here. Of course, if you google you can find more. You can always ask if needed.

    PS - Παρακαλώ
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    What I want to do, is to get a char type variable from the keyboard and compare it with the enum? Is that possible?

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes. Start the enum's first letter (A) with the corresponding ASCII value... that way you will get all the letters the correct ASCII value. Then read normally from input and just compare like this
    Code:
    if(c==A)
    and I think that would be enough
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Ok, but is possible to compare the char type variable with the enum at once? I mean, can I treat the enumerator as an array?
    For example:

    Code:
    enum converter {A = 65, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};
    
    for (k = 0; k < 26; k++)
          if (((var = strcmp(input[k], converter[k]))) == 0)
               x = 'converter[k]' - 64;
    
    Or somehow else?
    Last edited by Sotiris Kaniras; 03-05-2013 at 04:05 AM.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Are you guys sure that enums will work? AFAIK, in C it is not guaranteed that an enum is implicitly convertible to an integral type. Considering char is an integral type, this is important.

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Quote Originally Posted by whiteflags View Post
    Are you guys sure that enums will work? AFAIK, in C it is not guaranteed that an enum is implicitly convertible to an integral type. Considering char is an integral type, this is important.
    So?

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Well, Just to be clear, you don't need enums, at all.

    Even editing your own answer is close enough:

    Code:
    #include <ctype.h> /* provides tolower() */
    
    for (k = 0; k < 26; k++)
          if (tolower(input) == "abcdefghijklmnopqrstuvwxyz"[k])
               return k;
    Assuming input is a char...

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with hex to dec converter ??
    By XWR-AiA in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2010, 06:23 PM
  2. CD to MP3 Converter
    By Dark_Phoenix in forum Tech Board
    Replies: 5
    Last Post: 12-14-2006, 02:34 PM
  3. Key Converter
    By Insenic in forum C Programming
    Replies: 5
    Last Post: 02-02-2006, 11:17 PM
  4. Help with Error - Binary to Character Converter
    By dvldrmmr in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 01:21 PM
  5. c to c++ converter
    By kashifk in forum C Programming
    Replies: 1
    Last Post: 04-01-2003, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21