如何在字符串中检查每个字母以查看它们是否重新字母字符而不是数字?

发布于 2025-02-04 21:29:12 字数 1187 浏览 2 评论 0原文

我正在研究一个分配(C ++),必须让用户写出一串文本(示例输入:1hello22)。

任务也是按顺序删除数字。因此,输出将是:

  1. 1hello22
  2. Hello22 Hello2
  3. Hello2
  4. 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:

  1. 1hello22
  2. hello22
  3. hello2
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2025-02-11 21:29:12

您的代码需要许多Q ++表达式。您应该从(q = 0; q&lt; =(usertext.length()); q ++)和else> else子句中删除它。

此外,循环的条件必须为&lt;(不是&lt; =),因为字符串的最后一个字符具有索引长度() - 1

Your code has to many q++ expressions. You should remove it from for (q=0;q<=(userText.length());q++) and from the else clause.

Besides of that, condition of the for loop must be < (not <=), because the last character of a string has the index length() - 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文