Converting Letters to Numbers in C

前端 未结 10 1223
悲&欢浪女
悲&欢浪女 2020-11-30 07:22

I\'m trying to write a code that would convert letters into numbers. For example A ==> 0 B ==> 1 C ==> 2 and so on. Im thinking of writing 26 if statements. I\'m wondering

10条回答
  •  旧时难觅i
    2020-11-30 07:41

    Since the char data type is treated similar to an int data type in C and C++, you could go with some thing like:

    char c = 'A';   // just some character
    
    int urValue = c - 65;
    

    If you are worried about case senstivity:

    #include  // if using C++ #include 
    int urValue = toupper(c) - 65;
    

提交回复
热议问题