?
Solved

C#    how to get char - 'A'

Posted on 2010-11-10
8
Medium Priority
?
1,177 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 26

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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