Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C#    how to get char - 'A'

Posted on 2010-11-10
8
Medium Priority
?
1,181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 27

Accepted Solution

by:
Shaun Kline earned 320 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

598 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