How to check if character is letter in C++

1 Answer

0 votes
#include <iostream>
#include <cctype>
#include <string>

int main() {
    std::string s = "c++ programming";
    if (std::isalpha(s[0])) {
        std::cout << "yes" << std::endl;
    } else {
        std::cout << "no" << std::endl;
    }
    
    if (std::isalpha(s[2])) {
        std::cout << "yes" << std::endl;
    } else {
        std::cout << "no" << std::endl;
    }

    char ch = 'a';
    if (std::isalpha(ch)) {
        std::cout << "yes" << std::endl;
    } else {
        std::cout << "no" << std::endl;
    }

    std::cout << (std::isalpha('z') ? "yes" : "no");
}



/*
run:

yes
no
yes
yes

*/

 



answered Jan 2, 2025 by avibootz

Related questions

1 answer 85 views
85 views asked Jan 2, 2025 by avibootz
1 answer 187 views
1 answer 187 views
1 answer 89 views
1 answer 98 views
1 answer 91 views
1 answer 82 views
...