Solved

C#    how to get char - 'A'

Posted on 2010-11-10
8
1,164 Views
Last Modified: 2012-05-10
I am taking a string "str" full of capitals 'A' thru 'Z' which represents a 26 base number where 'A' = 0, 'B' = 1, and so on.....

part of this operation is a line like so:  

int place_number =  (int)(str[i] - 'A') * (26*(i))

Open in new window


essentially I just want the numerical difference between a character in the string and 'A', so that 'A' returns 0, 'B' returns 1, and so on....

I am more familiar with C++ that C#, so I'm not understanding the type conversion here.  The compiler keeps saying Error      1      A new expression requires (), [], or {} after type      

I don't understand that.  


0
Comment
Question by:LindyGeek
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:LindyGeek
ID: 34105507
So, how do I make the compiler happy and still get the result of str[i] - 'A'   ?
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 80 total points
ID: 34105636
Maybe this:
int place_number = (((int)str[i]) - ((int)'A')) * (26 * (i));
0
 
LVL 19

Expert Comment

by:elimesika
ID: 34105717
HI

this is because you defined str as a string array, you should define it as a char array
Example:

int i=0;
            char[] str =
            {
                'A',
                'B',
                'C'
            }
            ;
            int place_number = (int) (str[i] - 'A')*(26*(i));

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Closing Comment

by:LindyGeek
ID: 34105728
Cumbersome, but that did it!
0
 

Author Comment

by:LindyGeek
ID: 34105737
Sorry elimesika,

looks like I gave the solution credit as you were posting...
0
 
LVL 3

Expert Comment

by:yanoch
ID: 34105789
Hi friend,

Welcome to C# world! ;)

try this:

Convert.ToInt32(str[i] - 'A');
0
 
LVL 19

Expert Comment

by:elimesika
ID: 34105872
LindyGeek

that's ok, I am here to learn , not for the points ;-)
0
 
LVL 3

Expert Comment

by:yanoch
ID: 34106444
Btw, a string is indeed an array of char!

exemple:

[code]
            string myString = "abcdefghijklmnopqrstuvwxyz";
            Console.WriteLine("char at pos {0}:{1}", 10, myString[10]);
[/code]
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question