Solved

C#    how to get char - 'A'

Posted on 2010-11-10
8
1,162 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
 

Author Closing Comment

by:LindyGeek
ID: 34105728
Cumbersome, but that did it!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now