I have a character array that I have to completely check. It is necessary to make so that only alphabetic characters are checked, and specials. characters, numbers, spaces and unicode were not checked.

How to do it?

  • 2
    The most straightforward is the use of the standard C-function isalpha - Vlad from Moscow
  • 3
    but most likely, if this is a homework, this c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' - KoVadim
  • one
    And if it is unicode? What is more likely ... - NewView

1 answer 1

You can use the function std :: isalpha .

I also recommend reading the description in English cppreference.

This function will return a non-zero value if the argument is a letter in the current locale, if it is not a letter - returns 0. In the default locale it returns non-zero (true) for the characters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .

Example of use:

 std::string word; std::cin >> word; for (int i = 0; i < word.size(); ++i) { char x = word[i]; if (std::isalpha((unsigned char)x)) { // some code } } 

Another way, without using the standard library, but formally it is not guaranteed by the standard (although in practice, most likely, this will happen):

 bool isLetter(char x) { return (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'); } 

The latter method is not very good, because the standard does not guarantee that the letters go in order from 'A' to 'Z' (and similarly for lowercase).

  • I tried isalpha (). The error is already in the application itself. Expression: c> = - 1 && c <= 255 - Pon4iPay
  • one
    Please provide a piece of code that crashes with an error. Try to give the argument to unsigned char before calling the function: std :: isalpha ((unsigned char) x) - zcorvid
  • @HolyBlackCat everything works, but after the space, the cycle stops working. Those. All characters are processed by me, but as soon as the space is used, the program ends. In this case, all the specials. characters and numbers are skipped as they should - Pon4iPay
  • one
    Are you sure that there is something in your line after the space? If you read a line like this: std::cin >> str; , then reading is just before the gap. - zcorvid
  • How to fix it? - Pon4iPay