Is Your C++ Character Range Check Correct?

  • Thread starter gringo
  • Start date
  • Tags
    C++ Range
In summary, the conversation discusses difficulties with checking if an input character falls within a specified range. The individual has tried using ASCII and checking the pointer value but is unsure of the proper syntax and encountering errors. They suggest using the asterisk symbol to compare single characters instead.
  • #1
gringo
1
0
hi,
i have a little problem i m not able to check if an input character is in a specified range of characters. i.e is this char between A and Z let s say.
I tried to use the ascII but it didn t work out; no syntax errors at compile time but the error occurs at run time.
this is the code i used:
if(var<=75 && var >=65) //75 is letter K and 65 letter A
and var is a char *.

where is my problem?
THX
 
Physics news on Phys.org
  • #2
var is a char*? The value of a char* is a pointer, so you're checking if a pointer is less than 75, which doesn't make sense. I can't remember, but you might want to check something like &var or var& or var* or something like that. Again, my memory might be off, but should you have two ampersands? What is the actual error you're getting?
 
  • #3
First of all, a char* is a 32-bit hex value that points to some region of memory. To get the first value pointed to by var, you can either use *var or var[0].

If you want to compare single characters, you can use

Code:
if (*var <= 'K' || *var >= 'A')
without problems.
 

FAQ: Is Your C++ Character Range Check Correct?

What is the range of characters in C++?

The range of characters in C++ is determined by the standard character set, which includes ASCII characters (0-127) and extended ASCII characters (128-255). This means that there are a total of 256 possible characters in C++.

Can C++ handle non-ASCII characters?

Yes, C++ can handle non-ASCII characters through the use of extended ASCII characters. However, the specific support for non-ASCII characters may vary depending on the compiler and system being used.

What is the difference between a character and a string in C++?

A character in C++ represents a single symbol or letter, while a string is a collection of characters. A string is essentially an array of characters, allowing for the representation of longer sequences of text.

How do I declare a character variable in C++?

To declare a character variable in C++, you can use the char keyword followed by the variable name. For example: char letter = 'a'; This declares a variable named letter and assigns the value of 'a' to it.

Can a character variable hold multiple characters in C++?

No, a character variable in C++ can only hold a single character. However, you can use a string variable to hold multiple characters.

Similar threads

Back
Top