如何测试字符串的字符串 [英] how to test a string for letters only

查看:125
本文介绍了如何测试字符串的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字符串名称; 



cout<< 输入您的姓名
cin>>名称;

string letters =qwertyuiopasdfghjklzxcvbnm;

string :: iterator it;

for(it = name.begin(); it = name.end(); it ++)
{
size_t found = letters.find(it);
}


解决方案

STL方式:

  struct TestFunctor 
{
bool stringIsCorrect;
TestFunctor()
:stringIsCorrect(true)
{}

void operator()(char ch)
{
if(stringIsCorrect & ch(< ='z'& ch> ='a')||(ch <='Z'& ch> ='A'
stringIsCorrect = false;
}
}

TestFunctor functor;

for_each(name.begin(),name.end(),functor);

if(functor.stringIsCorrect)
cout<< 好极了;


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

string name;

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

string letters = "qwertyuiopasdfghjklzxcvbnm";

string::iterator it;

for(it = name.begin(); it = name.end(); it++)
{
  size_t found = letters.find(it);
}

解决方案

STL way:

struct TestFunctor
{
  bool stringIsCorrect;
  TestFunctor()
  :stringIsCorrect(true)
  {}

  void operator() (char ch)
  {
    if(stringIsCorrect && !((ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A')))
      stringIsCorrect = false;
  }
}

TestFunctor functor;

for_each(name.begin(), name.end(), functor);

if(functor.stringIsCorrect)
  cout << "Yay";

这篇关于如何测试字符串的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆