Question about limiting inputs in C

Click For Summary

Discussion Overview

The discussion revolves around how to limit inputs in a C program to only include capital and lowercase letters of the alphabet and the '#' sign when storing them in a two-dimensional array. Participants explore various methods for achieving this, including character comparisons and the use of standard library functions.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant suggests checking each input against a predefined array of valid characters, questioning the efficiency of this method.
  • Another participant points out that characters in C are represented by their ASCII codes and proposes checking if the input falls within the ASCII ranges for letters and the '#' sign.
  • A further contribution discusses using comparison operators for character ranges, illustrating with an example of how to compare characters directly.
  • One participant proposes a function that checks if a character is within the valid ranges or is the '#' sign, providing a code snippet for clarity.
  • Another participant emphasizes the use of character-testing functions from the ctype.h library, arguing that this approach is more portable and avoids assumptions about character encoding.
  • One participant mentions that functions like islower() and isupper() are designed to handle letters from various alphabets, not just English, suggesting their use for broader applicability.

Areas of Agreement / Disagreement

Participants express various methods for limiting inputs, with some advocating for direct character comparisons and others favoring the use of standard library functions. No consensus is reached on the best approach, as multiple valid methods are presented.

Contextual Notes

Some participants note that using direct character comparisons may depend on assumptions about character encoding, which can vary between implementations of C.

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   Reactions: 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   Reactions: jim mcnamara and newjerseyrunner

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · 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
4K
Replies
20
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K