how to test a string for letters only

后端 未结 6 534
盖世英雄少女心
盖世英雄少女心 2020-11-27 16:58

how could I test a string against only valid characters like letters a-z?...

string name;

cout << \"Enter your name\"
cin >> name;

string lette         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 18:00

    I would suggest investigating the ctype library: http://www.cplusplus.com/reference/std/locale/ctype/

    For example, the function is (see ctype.is) is a way to check properties on letters in locale sensitive manner:

    #include 
    using namespace std;
    bool is_alpha(char c) {
        locale loc;
        bool upper = use_facet< ctype >(loc).is( ctype::alpha, quote[0]);
        return upper;
    }
    

提交回复
热议问题