检查字符串的每个元素的数字或字符

问题描述:

我正在试着制作一个程序,告诉用户输入一个句子或其他东西。 然后,我需要查找字符串中有多少个数字,字符,所有其他字符(符号,句号,空格等)。检查字符串的每个元素的数字或字符

为什么我的代码不工作?我的事情与find_first_of一次检查整个字符串有关,而我希望它只检查索引“我”的位置。

[错误]请求构件 'find_first_of' 在“s1.std :: basic_string的< _CharT,_Traits,_Alloc> ::操作符[],标准::分配器>(((标准:: basic_string的:: size_type)i))',这是非类类型'char'是我在下面2条代码行中得到的错误。

#include <iostream> 
#include <string> 


using namespace std; 

int main() 
{ 
    string s1; 
    int numbers = 0; 
    int characters = 0; 
    int others = 0; 

    cout << "Please type in Something: "; 
    getline(cin, s1); 
    cout << "You typed: " << s1 << endl; 

    for(int i = 0; i < sizeof(s1)/sizeof(string); ++i) 
    { 
     if(s1[i].find_first_of("123456789") != string::npos) // Here is where the compiler errors 
     { 
      ++numbers; 
     } 
     else if(s1[i].find_first_of("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz") != string::npos) // And here with the same error 
     { 
      ++characters; 
     } 
     else 
     { 
      ++others; 
     } 
    } 
    cout << "Total numbers in the string are: " << numbers << endl << "Total characters in the string are: " << characters << endl << "Total special characters, symbols, spaces etc... are: "<< others; 

    return 0; 
} 
+0

使用'std :: isalpha()'和'std :: isdigit()'来检查一个字符是字母还是数字。没有必要使用'find_first_of' – PaulMcKenzie 2014-12-11 02:58:05

+0

谢谢我的朋友,我现在就试试。顺便说一句,你可以告诉我,find_first_of在这里实际上是错误的吗?就像为什么编译器不接受它?编辑:我不能仍然使用is alpha或isdigit,因为我得到相同的eroor.But如果我使用它像这样s1.find_first_of()或s1.isdigit()而不是s1 [i] .find_first_of和s1 [i]。 isdigit()然后它可以工作,但我需要检查每个特定的索引。所以我该怎么做? – Johnson 2014-12-11 03:00:18

+0

我更新了我的答案,为什么你的方法没有编译。 – PaulMcKenzie 2014-12-11 03:09:06

至于错误,s1[i]是单个字符,它不是一个std::string,因此试图用它作为一个字符串对象在调用find_first_of将无法​​正常工作(不编译)。


你的代码有至少其他两个问题:

问题1:

一个std::string知道它有多少个字符已通过调用其size()方法。因此,这是错误的:

for(int i = 0; i < sizeof(s1)/sizeof(string); ++i) 

它应该是:

for(size_t i = 0; i < s1.size(); ++i) 

问题2:用法string::find_first_of

是没有必要的。有确定一个字符是字母和数字的函数。

#include <cctype> 
    //... 
    if (std::isdigit(s1[i])) 
     ++numbers; 
    else 
    if (std::isalpha(s1[i])) 
     ++characters; 
    else 
     ++others; 

的std :: ISDIGIT:http://en.cppreference.com/w/cpp/string/byte/isdigit

的std ::因而isalpha:http://en.cppreference.com/w/cpp/string/byte/isalpha

+0

谢谢我的程序现在可以运行。 – Johnson 2014-12-11 03:12:07

存在诸多问题与代码: 1.You不能调用find_first_of上的字符 2。 find_first_of不会帮助

#include <iostream> 
#include <string> 
#include <cctype> 

using namespace std; 

int main() 
{ 
string s1; 
int numbers = 0; 
int characters = 0; 
int others = 0; 

cout << "Please type in Something: "; 
getline(cin, s1); 
cout << "You typed: " << s1 << endl; 

for(size_t i = 0; i < s1.size(); ++i) 
{ 
    if(isdigit(s1[i])){++numbers;}  
    else if(isalpha(s1[i])){++characters;} 
    else{ ++others;} 
} 

cout << "Total numbers in the string are: " << numbers << endl 
<< "Total characters in the string are: " << characters << endl 
<< "Total special characters, symbols, spaces etc... are: "<< others<<endl; 

return 0; 

}

+0

谢谢我的朋友。 – Johnson 2014-12-11 03:12:36