我编写了一个代码,以便它使用 isalpha() 函数删除除 alphabats 之外的所有内容(如空格和其他内容),并使用 tolower() 函数将其转换为小写。如果我不在字符串中放置空格,它工作正常,但如果字符串中有任何空格,则它超出了空格。我不明白为什么会这样。这是我写的代码。
#include<bits/stdc++.h>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
int i;
string A,b="";
cin>>A;
for(i=0;i<A.size();i++)
{
if(isalpha(A[i]))
b+= tolower(A[i]);
else
continue;
}
cout<<b;
}
Run Code Online (Sandbox Code Playgroud)
请帮我。谢谢
我写了以下代码:
#include<bits/stdc++.h>
using namespace std;
int lengthOfLastWord(string A) {
int m= A.size();
int i=0;
while(i<m)
{
if(A[i]==' ')
{
int count=0;
i++;
while(A[i]!=' ')
{
count++;
i++;
}
}
else
i++;
}
return count;
}
int main()
{
string A;
getline(cin,A);
cout<<lengthOfLastWord(A);
}
Run Code Online (Sandbox Code Playgroud)
但它显示以下错误
error: cannot resolve overloaded function ‘count’ based on conversion to type ‘int’
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么它会显示此错误以及我应该如何解决它。请帮我。谢谢