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
Solved

Which letter or digit ??

Posted on 2000-03-25
2
284 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 84
C++ to C# code conversion issue 4 111
How to split this in C++ 4 106
computer science syllabus 3 88
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

792 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