ACM/ICPC 常用函数---strstr()字符串查找函数
2014-03-13 00:10
295 查看
char *strstr(char *str1, char *str2)
该函数的作用是在字符串str1中寻找str2字符串的位置,并返回指向该位置的指针,如果没有找到相匹配的就返回空指针;
Here is a sample program for the use of the function:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#define MAXN 256
using namespace std;
int main()
{
char str1[MAXN], str2[MAXN];
while(~scanf("%s %s", str1, str2)) {
char *pos = strstr(str1, str2); //注意:该函数返回的是一个char 类型的指针,而不是int型的;
if(pos != NULL) puts("YES");
else puts("NO");
}
return 0;
}
HLG 1620 小Z的卡片----
链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1620
当然这道题还可以用到C++ set容器, 用set <string> s; s,find(s2);查找函数即可; C++set容器的具体用法请看我的博客C++ 容器笔记;
该函数的作用是在字符串str1中寻找str2字符串的位置,并返回指向该位置的指针,如果没有找到相匹配的就返回空指针;
Here is a sample program for the use of the function:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#define MAXN 256
using namespace std;
int main()
{
char str1[MAXN], str2[MAXN];
while(~scanf("%s %s", str1, str2)) {
char *pos = strstr(str1, str2); //注意:该函数返回的是一个char 类型的指针,而不是int型的;
if(pos != NULL) puts("YES");
else puts("NO");
}
return 0;
}
HLG 1620 小Z的卡片----
链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1620
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 105 using namespace std; typedef struct Node_ //Node后面的下划线只是个人编程的习惯;非语法需要; { char s[MAXN]; }Node; Node N[15]; char str[MAXN]; int main() { int cas, n; scanf("%d", &cas); getchar(); //吸收换行符; while(cas--) { scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%s", N[i].s); } scanf("%s", str); int count = 0; for(int i=0; i<n; i++) { char *pos = strstr(str, N[i].s); //定义字符型的位置指针;该指针指向s2在s1中出现的位置; if(pos != NULL) count++; //如果pos不为空,则表示找到了;否则没找到; } printf("%d\n", count); } return 0; }
当然这道题还可以用到C++ set容器, 用set <string> s; s,find(s2);查找函数即可; C++set容器的具体用法请看我的博客C++ 容器笔记;
相关文章推荐
- php中常用的字符串查找函数strstr()、strpos()实例解释
- php中常用的字符串查找函数strstr()、strpos()实例解释
- php中常用的字符串查找函数strstr()、strpos()实例解释
- php字符串常用处理函数(数组的拆分、查找替换)
- php strstr查找字符串中是否包含某些字符的查找函数
- C语言字符与字符串查找函数strstr、wcsstr(wcswcs)、_tcsstr
- C语言:模拟实现字符串中查找一个字符串my_strstr()函数
- ColdFusion中常用几种字符串查找函数
- 一个自己实现的strstr 字符串查找函数,大家看看有什么错误和改进的地方
- PHP 查找字符串常用函数介绍
- 常用字符串查找函数
- 使用strstr()函数在一个字符串中查找另一个字符串
- C语言strstr()函数常用示例详解[求一个字符串在另一个字符串中出现的次数]
- 分享给大家两个不区分大小写的字符串查找函数 strstr
- PHP 查找字符串常用函数介绍
- strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
- 模拟实现字符串查找函数strstr
- 字符串比较函数strncasecmp():比较字符串的前n个字符|字符串查找函数strstr和string.find() 查找字符串str1中是否存在与str2一样的子字符串
- VC 中常用字符串比较和查找函数