Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Which letter or digit ??

Posted on 2000-03-25
2
Medium Priority
?
288 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
[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
2 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 150 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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