如何在字符串中检查每个字母以查看它们是否重新字母字符而不是数字?
我正在研究一个分配(C ++),必须让用户写出一串文本(示例输入:1hello22)。
任务也是按顺序删除数字。因此,输出将是:
- 1hello22
- Hello22 Hello2
- Hello2
- Hello 2
如何检查此循环中的每个字符串字母?我无法重复自己。我假设我需要在这里使用嵌套的循环,但是我不确定如何进行。
这是我到目前为止的一切:
cout<<"Enter some text:";
cin.ignore();
getline(cin,userText);
system("clear");
for (q=0;q<=(userText.length());q++)
{
if (isalpha(userText.at(q))) //checks for alphabet
{
q++;
cout<<userText<<endl;
}
else
{
userText.erase(q,1); //gets rid of number
q++;
cout<<userText<<endl;
}
}
I'm working on an assignment (C++) where I have to have the user write out a string of text (Example input: 1hello22).
The task is to remove the digits one by one, in order too. So the output would be something like:
- 1hello22
- hello22
- hello2
- hello
How do I check each string letter in this loop? I can't get it to repeat itself. I'm assuming I need to use a nested for loop here, but I'm stuck and I'm not sure how to proceed.
Here's what I've got so far:
cout<<"Enter some text:";
cin.ignore();
getline(cin,userText);
system("clear");
for (q=0;q<=(userText.length());q++)
{
if (isalpha(userText.at(q))) //checks for alphabet
{
q++;
cout<<userText<<endl;
}
else
{
userText.erase(q,1); //gets rid of number
q++;
cout<<userText<<endl;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码需要许多
Q ++
表达式。您应该从(q = 0; q&lt; =(usertext.length()); q ++)和else> else
子句中删除它。此外,循环的
条件必须为
&lt;
(不是&lt; =
),因为字符串的最后一个字符具有索引长度() - 1
。Your code has to many
q++
expressions. You should remove it fromfor (q=0;q<=(userText.length());q++)
and from theelse
clause.Besides of that, condition of the
for
loop must be<
(not<=
), because the last character of a string has the indexlength() - 1
.