Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Is there an easier way to write this in c++?
I want to right an if statement that says "if i is equal to any of the punctuation marks," do whatever.
Is there an easier way than:
if (i=='.' || i=='!' || i=='?' etc.)
2 Answers
- PaulLv 49 years agoFavorite Answer
There is an easier way.
If you look at an ASCII table, you will notice all the punctuation symbols are between 33 and 47 where "!" is the 33rd character and "/" is the 47th character.
So you can just check if the character is between 33 and 47.
ie
if (i >= '!' && i <= '/')
{
// i is punctuation
}
Source(s): http://www.asciitable.com/ - SteveOLv 79 years ago
No there is not, unless you want to use a switch block which wouldn't really be well suited if you're just testing if a punctuation mark is in some string. You could use a regular expression if you wanted.
EDIT: Yeah, forgot about the ASCII table...I guess that's what happens when I'm tired. That is an excellent solution to this.