Switch语句对输入的字符串求值
Switch statement to evaluate string input C++
我正在处理用户输入的字符串输入,我需要使用开关语句来计算每个输入的输入。下面的代码当前计算用户字符串输入,并使用ASCII码查看它是大写字母、数字还是特殊字符。我现在确定了switch语句是如何工作的,以及如何将If语句更改为switch语句。
for (int i = 0; i < strlength; i++) //for loop used to check the rules of the password inputted by the user
{
cout << "Testing for upper case characters..." << endl; //displays the cout
tmpi=(int) str1[i]; //stoi function making the string input an integer
if ((tmpi >= 65) && (tmpi <= 90)) //checks if there are two upper case characters in the string
{
cout << "Found an uppercase" << endl;
uppercnt++; //adds to the counter of upper case
state++;
cout << "Now in state q" << state << "..." << endl;
continue;
}
cout << "Testing for digits..." << endl;
if(tmpi >= 48 && tmpi <= 57) //checks if there are two digits in the string
{
cout << "Found a digit" << endl;
digitcnt++; //adds to the counter of digit
state++;
cout << "Now in state q" << state << "..." << endl;
continue;
}
cout << "Testing for special characters..." << endl;
if(tmpi >= 33 && tmpi <= 47 || tmpi >= 58 && tmpi <= 64 || tmpi >= 91 && tmpi <= 96 || tmpi >= 123 && tmpi <= 126) //checks if there are special characters
{
cout << "Found a special char" << endl;
speccnt++; //adds to the counter of special character
state++;
cout << "Now in state q" << state << "..." << endl;
continue;
}
cout << "Character entered was a lower case" << endl;
state++;
cout << "Now in state q" << state << "..." << endl;
} //end for loop
任何建议或例子都会有所帮助,谢谢。
如果性能不是问题,我就使用std::count_if
:
int upps = std::count_if( pass.begin(), pass.end(), isupper );
int digs = std::count_if( pass.begin(), pass.end(), isdigit );
ideone的工作示例
相关文章:
- 如何使用堆栈将字符串输入拉伸 k 次
- 不确定出了什么问题(字符串输入)
- 如何获取字符串输入n次?
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
- C++ 对象数组字符串输入在控制台上不起作用
- 为什么 cin 在包含字符串标头后接受字符串输入
- 拆分字符串输入并查找字符是否有效
- 有没有办法将最初作为字符串输入的数字字符数组复制到双向链表中
- 将字符串输入到多维数组中
- 字符串输入后的空格
- 字符串流中的自定义字符串输入
- C 未验证字符串输入
- 总结数字字符串输入的最佳方法
- 如何验证用户将字符串输入到std :: cin中
- 在C++测试家庭作业问题中的字符串输入时无法退出循环(作为数据验证)
- 如何在C++中检测字符串输入中的换行符
- C++将字符串输入拆分为两个整数
- 如何检查字符串输入是否为有效的双精度
- 在读取数据文件时,如何找到用户字符串输入的字谜?