Question about limiting inputs in C

In summary, the easiest way to do this is to check for a character in the range of numbers corresponding to the letters of the alphabet or #.
  • #1
doktorwho
181
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
  • #2
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
  • #3
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().
 
  • #4
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;
}
 
  • #5
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

1. How do I limit the number of inputs in a C program?

To limit the number of inputs in a C program, you can use a loop and a counter variable. In each iteration of the loop, prompt the user for input and increment the counter. Once the counter reaches the desired limit, you can exit the loop and continue with the rest of the program.

2. Can I limit the type of inputs in a C program?

Yes, you can limit the type of inputs in a C program by using conditional statements and data validation. For example, you can use the scanf() function to only accept integers or use the isdigit() function to check if the input is a digit before storing it in a variable.

3. How can I handle errors if the user inputs more than the allowed limit?

You can handle errors in a C program by using the fflush() function to clear the input buffer or by using a combination of fgets() and sscanf() to read and parse the input. Alternatively, you can also use the scanf() function to check the return value and handle errors accordingly.

4. Is there a way to dynamically change the limit of inputs in a C program?

Yes, you can use variables to dynamically change the limit of inputs in a C program. This can be useful when you want to adjust the limit based on certain conditions or user preferences. You can also use command-line arguments to specify the limit before running the program.

5. How can I limit the number of characters in each input in a C program?

To limit the number of characters in each input in a C program, you can use the fgets() function and specify the maximum number of characters to read. You can also use string manipulation functions like strlen() to check the length of the input string and handle errors if it exceeds the desired limit.

Similar threads

  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
2
Views
967
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
1
Views
935
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top