Question about limiting inputs in C

Click For Summary
To create a program that stores only capital and lowercase letters of the alphabet and the '#' sign in a two-dimensional array, an efficient approach involves checking each input character against specific criteria. Instead of manually comparing characters to predefined ranges, utilizing C's standard library functions from ctype.h, such as isalpha(), islower(), and isupper(), is recommended. These functions simplify the process by determining if a character is an alphabet letter or the '#' sign without relying on ASCII values directly. A suggested implementation is to define a function that checks if a character is either an uppercase letter, a lowercase letter, or the '#' sign. This method is not only more efficient but also enhances code portability by avoiding assumptions about character encoding, ensuring compatibility across different systems.
doktorwho
Messages
181
Reaction score
6
I want to write a program that in the twodimensional array stores only capital and noncapital letters of the alphabet and the sign #. Everything else inputted cannot be stored in the array. How am i to do this in the easiest way?
I can think of only checking for each input if it exists in some array i created with only those letters and the sign but that seems like a lot of work. Is there an easier way that i don't know of?
 
Technology news on Phys.org
Characters are stored in C as numbers according to their ASCII codes. Just check to see if each input character falls within the range of numbers corresponding to the letters of the alphabet or #.
 
  • Like
Likes doktorwho
You can compare single chars using the same comparison operators as for numbers, e.g.

Code:
char first = 'f';  // or read it in with a scanf() or whatever
if (first > 'a')
{
    // do something
}
else
{
   // do something else
}

For lowercase letters, compare for the range from 'a' to 'z'; for uppercase, 'A' to 'Z', just like you would for a range of numbers.

You might also investigate the character-testing functions in ctype.h, e.g. isalpha(), islower(), isupper().
 
You have two ranges and a discrete value that it can be, so do an if.

C:
int isInputValue(char value){
     if (value >= 'A' && value <= 'Z') return 1;
     if (value >= 'a' && value <= 'z') return 1;
     return (value == '#') ? 1 : 0;
}
 
jtbell said:
You might also investigate the character-testing functions in ctype.h, e.g. isalpha(), islower(), isupper().

This is the portable way to do it. (As well as not reinventing functionality already in the standard library.)
C:
#include <ctype.h>

int is_alpha_or_hash(int c)
{
    return isalpha(c) || c == '#';
}
Doing things like c >= 'A' && c <= 'Z' is making the code dependent on assumptions about the character encoding used by the C compiler (which is implementation-defined, even if many implementations use ASCII or similar in practice).
 
  • Like
Likes jim mcnamara and newjerseyrunner
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
20
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K