字符串查找函数 find()函数
find()函数可以帮助你在两个字符串之间,查找很多他们的关系。。。
#include<iostream>
#include<string>
using namespace std; int main()
{ string s,s2;
int n;
cin>>n;
while(n--)
{
cin>>s>>s2;
string flag;
int position = s.find(s2);
if (position != s.npos) cout << "position is : " << position << endl; else cout << "Not found the flag" + flag;
}
return 0;
}
/查找s2在s中的第一个位置
#include<iostream>
#include<string>
using namespace std; int main()
{ string s,s2;
int n;
cin>>n;
while(n--)
{
cin>>s>>s2;
string flag = s2;
int position = s.find_first_of(flag);
cout << "s.find_first_of(flag) is : " << position << endl;
}
return 0;
}
#include<iostream>
#include<string>
using namespace std; int main()
{ string s,s2;
int n,m;
cin>>n;
while(n--)
{
cin>>s>>s2;
cin>>m;
int position=s.find(s2,m);//在s中从第n的位置开始查找第一个s2的位置
cout<<"s.find(s2,m) is : "<<position<<endl;
}
return 0;
} #include<iostream>
#include<string>
using namespace std; int main()
{ string s,s2;
int n;
cin>>n;
while(n--)
{
cin>>s>>s2;
string flag=s2;
int position=0;
int i=1;
while( (position=s.find_first_of(flag,position) )!=string::npos )//在s中查找所有s2的位置并依次输出
{
//position=s.find_first_of(flag,position);
cout<<"position "<<i<<" : "<<position<<endl;
position++;
i++;
}
}
return 0;
}
//在s中从最后一位开始查找s2的第一个位置
#include<iostream>
#include<string>
using namespace std; int main()
{ string s,s2;
int n;
cin>>n;
while(n--)
{
cin>>s>>s2;
string flag=s2;
int position=0;
position=s.rfind (flag);
cout<<"s.rfind (flag) :"<<position<<endl;
}
return 0;
}*/
在以上的例子中,我们可以熟练掌握一些题型。
http://acm.nyist.net/JudgeOnline/problem.php?pid=5
代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2;
int n;
cin>>n;
while(n--)
{
cin>>s1>>s2;
unsigned int m=s2.find(s1,0);
int num=0;
while(m!=string::npos)
{
num++;
m=s2.find(s1,m+1);
}
cout<<num<<endl;
}
return 0;
}
字符串查找函数 find()函数的更多相关文章
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- C/C++字符串查找函数
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- C/C++字符串查找函数 <转>
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...
- 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)
1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...
- javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数
javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数 1.常规函数 javascript常规函数包括以下9个 ...
- c/c++中与字符串处理相关的函数
void *memccpy (void *dest, const void *src, int c, size_t n); 从src所指向的对象复制n个字符到dest所指向的对象中.如果复制过程中遇到 ...
- JS 字符串对象 数组对象 函数对象 函数作用域
一.内置对象 object对象:ECMAScript 中的所有对象都由这个对象继承而来:Object 对象中的所有属性和方法都会出现在其他对象中 ToString() : 返回对象的原始字符串表示.V ...
- Java字符串常见实例与函数
字符串比较 字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 来比较两个字符串,并返回字 ...
- (查找函数+atoi)判断与(注册函数+strcmp函数)判断两种方法
loadrunner中接口判断的2中方法 如下: 1. ●查找函数web_reg_find() ● atoi():将字符串转换为整型值 作比较 > 0 Action() { //检查点函 ...
随机推荐
- Page Cache, the Affair Between Memory and Files
Previously we looked at how the kernel manages virtual memory for a user process, but files and I/O ...
- GDI+ 中发生一般性错误(在 OutputStream 中保存 PNG 格式图像时遇到的问题)
在将图片以 PNG 格式保存至 Response.OutputStream 时,会碰到如下错误: GDI+ 中发生一般性错误. 原因: 在写 PNG 格式的图像时,指针需要在存储的位置来回移动.而 R ...
- 手动修改VisualStudio IISExpress的配置
<VisualStudio> <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}"> ...
- C/C++中的内存管理
一.内存的分配方式 1. 程序代码区 2. 静态数据区 3. 动态数据区 二.动态内存 1. 在栈上创建的内存 2. 从堆上分配的内存 3. 小结 三.指针与内存 1. 操作内存 2. 指针与数组 3 ...
- 开源项目live555学习心得
推荐:伊朗美女找丈夫比找工作难女人婚前一定要看清三件事 × 登录注册 疯狂少男-IT技术的博客 http://blog.sina.com.cn/crazyboyzhaolei [订阅][手机订 ...
- JAVA GUI学习 - JTabbedPane选项卡组件学习
public class JTabbedPaneKnow extends JFrame { JTabbedPane jTabbedPane; JPanel jPanelRed; JPanel jPan ...
- 笔试题引出float数据的存储方式的深究
笔试题: #include <iostream>#include <stdio.h>#include <string.h>#include <conio.h& ...
- xpage 获取 附件
var db:NotesDatabase=session.getCurrentDatabase(); var doc:NotesDocument=db.getDocumentByUNID('80E21 ...
- DevExpress ASP.NET 使用经验谈(6)-ASPxGridView属性设置与CRUD界面优化
上一节中,我们通过简单的配置,通过ASPxGridView控件的使用,完成了对数据库表的CRUD操作. 这样的界面展现,功能是达到了,但是操作体验上,还是有所欠缺的. 图一 默认生成的列表界面 图二 ...
- Eclipse新建Android工程,在模拟器运行的时候提示Unfortunately,XXX has stopped.
刚新建好的android工程在模拟器运行的时候出错,提示Unfortunately,XXX has stopped 查看Eclipse下面的错误信息,双击第一条 把ActionBarActivity前 ...