how to test a string for letters only

后端 未结 6 527
盖世英雄少女心
盖世英雄少女心 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 17:33

    C++11 approach using std::all_of:

    std::all_of(std::begin(name), std::end(name),
        [](char c){ return std::isalpha(c); });
    

    std::all_of will only return true if all of the elements are true according to the supplied predicate function.

提交回复
热议问题