Solved

Which letter or digit ??

Posted on 2000-03-25
2
282 Views
Last Modified: 2010-04-02
How can i determine the  letter or digit from its ASCII code easily/efficiently. I  currently use the switch statement and i have 26 cases for letter and 10 for digit.
i.e.
 switch (letr)
      {
     case 97:
     {
      strcpy(ltr,"a");
      ltr[1] = '\0';
      strcat(expression_buffer,ltr);
      ltr[0] = '\0';
     };break;//end
     .
     .
     .
     case 122:
     {
      strcpy(ltr,"z");
      ltr[1] = '\0';
      strcat(expression_buffer,ltr);
      ltr[0] = '\0';
     };break;//end


and the same thing for digits!!!!

This  makes my code too big

Pls help me find a better way of doing this.

I'm using turbo C++

All suggestions are welcome.

Thanks in advance.
0
Comment
Question by:onalenna
2 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 50 total points
ID: 2657433
You could just place the ASCII character in your character array. For example:

if (isalpha(letr) || isdigit(letr))
  {
  ltr[0] = letr;
  ltr[1] = '\0';
  }
0
 
LVL 22

Expert Comment

by:nietod
ID: 2657583
Also (and this is a step in the wrong direction, because Steve's suggestion is clearly better, but for future reference) you can always use a single character in place a number representing the character's aSCII value, for example you could have done

 switch (letr)
{
case 'a':
{
strcpy(ltr,"a");
ltr[1] = '\0';
strcat(expression_buffer,ltr);
ltr[0] = '\0';
};break;//end
case 'b':

etc.

That should be much clearer to understand.  Obviously, don't do it now!
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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

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

19 Experts available now in Live!

Get 1:1 Help Now