C++ 拆分字符串输入并查找字符是否有效
接下来可以执行以下操作: 假设我有一个字符串输入,这将是一个输入,我将把这个输入分为两部分,接下来,我将发现我是否只输入了第一部分的字母,第二部分的数字?该代码仅适用于字母,不适用于数字删除注释,以确保输入的所有内容都有效C++ 拆分字符串输入并查找字符是否有效,c++,C++,接下来可以执行以下操作: 假设我有一个字符串输入,这将是一个输入,我将把这个输入分为两部分,接下来,我将发现我是否只输入了第一部分的字母,第二部分的数字?该代码仅适用于字母,不适用于数字删除注释,以确保输入的所有内容都有效 #include <iostream> #include <string> #include <iomanip> #include <cctype> using namespace std; int main () {
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
int main ()
{
while (true)
{
bool flag = false; // to check for numeric entry
string input; // not req to initialize
string input1;
cout << "Enter the string like ABC 123: ";
getline (cin, input);
if (input == "")
{
flag = true;
}
if (string::size_type pos = input.find (' '))//spliting the input in 2 if it will find a space
{
if (input.npos != pos)
{
input1 = input.substr (pos + 1);
input = input.substr (0, pos);
}
}
for (int i = 0; i < input.size (); i++)
{
// for (int n = 0; i < input1.size (); i++)
// {
int uppercaseCHar = toupper (input[i]);//checking if input(first part) contains only letters
if (!std::isalpha (uppercaseCHar))
{
// if(isdigit(input1[n]) == 0)//checing if input1(second part) contains only digits
// {
flag = true;
break;
// }
}
// }
}
if (input.compare ("1") == 0) break;//This will end program
{
flag = false;
}
if (flag)
{
cout << "Invalid!\n";
cout << endl;
} else
{
cout << "The string is valid! \n";
cout << endl;
}
}
}
输入类似ABC 123:QWE 123的字符串
字符串是有效的
输入类似ABC 123:QW1 123的字符串
无效 我还没有评论的权限,但是如果字符串的第一部分和第二部分不必具有相同的长度,可以对每个子字符串使用for循环两次,然后一次比较一个字符的值。 同样,在通过以下方式检查终止条件之后:
if (input.compare ("1") == 0) break;
您正在添加一条语句
{
flag = false;
}
这会将结果设置为false,即使您在循环中进行了比较并发现结果为TRUE,所以请看下面的代码,我已经注释掉了这段代码
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
int main ()
{
while (true)
{
bool flag = false; // to check for numeric entry
string input; // not req to initialize
string input1;
cout << "Enter the string like ABC 123: ";
getline (cin, input);
if (input == "")
{
flag = true;
}
if (string::size_type pos = input.find (' '))//spliting the input in 2 if it will find a space
{
if (input.npos != pos)
{
input1 = input.substr (pos + 1);
input = input.substr (0, pos);
}
}
//cout<<"\n"<<input1;
//cout<<"\n"<<input;
//First check the letter part (first part) if it contains digits
for(int i=0;i<input.size();i++){
if(!std::isalpha(input[i])){
flag=true;
break;
}
}
//second check if the numeric part (second part) only contains digits
for(int i=0;i<input1.size();i++){
if(!std::isdigit(input1[i])){
flag=true;
break;
}
}
/*
for (int i = 0; i < input.size (); i++)
{
for (int n = 0; i < input1.size (); i++)
{
int uppercaseCHar = toupper (input[i]);//checking if input(first part) contains only letters
if (!std::isalpha (uppercaseCHar))
{
if(isdigit(input1[n]) == 0)//checing if input1(second part) contains only digits
{
flag = true;
break;
}
}
}
}
*/
if (input.compare ("1") == 0) break;//This will end program
//after checking for break, if you add flag=false, it will automatically ignore whatever flag you set, and you will always find FLAG=FALSE when compairing in upcoming lines
//{
// flag = false;
//}
if (flag)
{
cout << "Invalid!\n";
cout << endl;
} else
{
cout << "The string is valid! \n";
cout << endl;
}
}
}
我还没有评论的权限,但是如果字符串的第一部分和第二部分不必具有相同的长度,那么可以对每个子字符串使用for循环两次,然后一次比较一个字符的值。 同样,在通过以下方式检查终止条件之后:
if (input.compare ("1") == 0) break;
您正在添加一条语句
{
flag = false;
}
这会将结果设置为false,即使您在循环中进行了比较并发现结果为TRUE,所以请看下面的代码,我已经注释掉了这段代码
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
int main ()
{
while (true)
{
bool flag = false; // to check for numeric entry
string input; // not req to initialize
string input1;
cout << "Enter the string like ABC 123: ";
getline (cin, input);
if (input == "")
{
flag = true;
}
if (string::size_type pos = input.find (' '))//spliting the input in 2 if it will find a space
{
if (input.npos != pos)
{
input1 = input.substr (pos + 1);
input = input.substr (0, pos);
}
}
//cout<<"\n"<<input1;
//cout<<"\n"<<input;
//First check the letter part (first part) if it contains digits
for(int i=0;i<input.size();i++){
if(!std::isalpha(input[i])){
flag=true;
break;
}
}
//second check if the numeric part (second part) only contains digits
for(int i=0;i<input1.size();i++){
if(!std::isdigit(input1[i])){
flag=true;
break;
}
}
/*
for (int i = 0; i < input.size (); i++)
{
for (int n = 0; i < input1.size (); i++)
{
int uppercaseCHar = toupper (input[i]);//checking if input(first part) contains only letters
if (!std::isalpha (uppercaseCHar))
{
if(isdigit(input1[n]) == 0)//checing if input1(second part) contains only digits
{
flag = true;
break;
}
}
}
}
*/
if (input.compare ("1") == 0) break;//This will end program
//after checking for break, if you add flag=false, it will automatically ignore whatever flag you set, and you will always find FLAG=FALSE when compairing in upcoming lines
//{
// flag = false;
//}
if (flag)
{
cout << "Invalid!\n";
cout << endl;
} else
{
cout << "The string is valid! \n";
cout << endl;
}
}
}
似乎问题在于n和iOh,这是我的错,但我改变了它,同样的结果,它只是没有检查input1second部分中的内容,我尝试了debug,但没有发现任何特殊的问题,比如n和iOh的问题,这是我的错,但我改变了它,同样的结果,它只是没有检查input1second部分中的内容,我试过调试,但没有发现什么特别的地方,好的,所以我需要做一个单独的for循环,用数字验证第二部分,好的,我得到了它,它按照需要工作。非常感谢,我将继续编写代码的其余部分,只是为了一个小的传统选择,我还需要一大行代码哦,好的,所以我需要做一个单独的for循环来验证第二部分的数字,好的,我知道了,它可以按需工作。非常感谢,我将继续编写其余的代码,只是为了一个小的传统选择,我还需要一大行代码