2012-12-15 33 views
1

把这个字符串:Separing在单独的单词串

asking a question is easy 

我想在一个while循环

  • 采取第一个字asking
  • 检查一些有关它的功能,
  • 然后转到下一个单词a
  • 等等直到字符串结尾

单词可以被一个或多个空格隔开。

编辑:

我想解释时,我竟然犯了一个错误。在这里我一直在努力:

int main() 
{ 
    cout<<"string="; gets(string); 
    cout<<"template="; cin>>template; 
    while (i<strlen(string)) 
    { 
     k=0; 
     while (string[i]!=' ') 
     { 
      word[k]=string[i]; 
      i++; k++; 
     } 
     if (function(string,word)==1) count++; 
     while (i<strlen(s) && string[i]==' ') 
      i++; 
    } 
    cout <<"count="<<count; 
} 

我想坚持一些旧的/基本的C + +,就像我在学校做的。

回答

0

把你的字符串中std::stringstream,然后从中提取词在while环(来检查流仍然没有问题):

std::string str("asking a question is easy"); 
std::stringstream ss(str); 
std::string word; 
while (ss >> word) { 
    // Do something 
} 
相关问题