如何判断一个汉字词包含指定的某个汉字
程序功能:从txt文档中逐行读入汉字词,然后判断这些词中那些含有特定的字。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
main()
{int i=0,j;
string str2= "你好 ";
ifstream in( "word.txt ");
for(string str1;getline(in,str1);) /*从文档中读入汉字*/
{ if(substr(str1,str2)> 0) ????这一句出错,应该怎么写呢?????
printf( "包含 ");
else printf( "不包含 ")
}
}
word.txt的内容是汉字;每一行有2-4个字组成。
提示错误信息:
error C2065: 'substr ' : undeclared identifier
怎样才能实现以上功能,哪位高人能给出一个完整的程序。万分感谢!!
[解决办法]
if(substr(str1,str2)> 0) ????这一句出错,应该怎么写呢?????
==》
if(str1.find(str2) != string::npos) //如果在 str1 中找到了 str2,则包含
[解决办法]
find
Syntax:
#include <string>
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );
The function find() either:
returns the first occurrence of str within the current string, starting at index, string::npos if nothing is found,
returns the first occurrence of str within the current string and within length characters, starting at index, string::npos if nothing is found,
or returns the index of the first occurrence ch within the current string, starting at index, string::npos if nothing is found.
For example:
string str1( "Alpha Beta Gamma Delta " );
string::size_type loc = str1.find( "Omega ", 0 );
if( loc != string::npos )
cout < < "Found Omega at " < < loc < < endl;
else
cout < < "Didn 't find Omega " < < endl;