C语言之strstr函数

FROM MSDN && 百科】

原型:char *strstr(const char *str1, const char *str2);

#include<string.h>

找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。

Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear instr. If strSearch points to a string of zero length, the function returns str.

DEMO: mystrstr



[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. #include <string.h>  
  4. #include <stdlib.h>  
  5. #pragma warning (disable:4996)  
  6. char *mystrstr(char *s1,char *s2);  
  7. int main(void)  
  8. {  
  9.     char *s="Golden Global View";  
  10.     char *l="ob";   //char *l=""  
  11.     char *p;  
  12.     system("cls");  
  13.     p=mystrstr(s,l);  
  14.     if (p!=NULL)  
  15.     {  
  16.         printf("%s\n",p);  
  17.     }  
  18.     else  
  19.     {  
  20.         printf("Not Found!\n");  
  21.     }  
  22.     getch();  
  23.     return 0;  
  24. }  
  25. /*FROM 百科*/  
  26. char *mystrstr(char *s1,char *s2)  
  27. {  
  28.     int n;  
  29.     if (*s2)                      //两种情况考虑  
  30.     {  
  31.         while(*s1)                 
  32.         {  
  33.             for (n=0;*(s1+n)==*(s2+n);n++)  
  34.             {  
  35.                 if (!*(s2+n+1))            //查找的下一个字符是否为'\0'  
  36.                 {  
  37.                     return (char*)s1;  
  38.                 }  
  39.             }  
  40.             s1++;  
  41.         }  
  42.         return NULL;  
  43.     }  
  44.     else  
  45.     {  
  46.         return (char*)s1;  
  47.     }  
  48. }  

DEMO:

[cpp] view plaincopy
  1. //#define FIRST_DEMO  
  2. #define SECOND_DEMO  
  3.   
  4. #ifdef FIRST_DEMO  
  5. #include <stdio.h>  
  6. #include <conio.h>  
  7. #include <string.h>  
  8. int main(void)  
  9. {  
  10.     char *s="Golden Global View";  
  11.     char *l="lob";  
  12.     char *p;  
  13.     system("cls");  
  14.     p=strstr(s,l);  
  15.     if (p!=NULL)  
  16.     {  
  17.         printf("%s\n",p);  
  18.     }  
  19.     else  
  20.     {  
  21.         printf("Not Found!\n");  
  22.     }  
  23.   
  24.     getch();  
  25.     return 0;  
  26. }  
  27. #elif defined SECOND_DEMO  
  28. /*从字串” string1 onexxx string2 oneyyy”中寻找”yyy”*/  
  29. #include <stdio.h>  
  30. #include <conio.h>  
  31. #include <string.h>  
  32. int main(void)  
  33. {  
  34.     char *s="string1 onexxx string2 oneyyy";  
  35.     char *p;  
  36.     p=strstr(s,"string2");  
  37.     printf("%s\n",p);  
  38.     if (p==NULL)  
  39.     {  
  40.         printf("Not Found!\n");  
  41.     }  
  42.     p=strstr(p,"one");  
  43.     printf("%s\n",p);  
  44.     if (p==NULL)  
  45.     {  
  46.         printf("Not Found!\n");  
  47.     }  
  48.     p+=strlen("one");  
  49.     printf("%s\n",p);  
  50.   
  51.     getch();  
  52.     return 0;  
  53. }  
  54. #endif  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/510276.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言fwrite函数了解

fwrite()函数----write data to a stream 原型&#xff1a; size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream); 注意&#xff1a;这个函数以二进制形式对文件进行操作&#xff0c;不局限于文本文件 demo: [cpp] view plaincopy #include <std…

Python-OpenCV 处理图像(一):基本操作 cv2

0x00. 图片读、写和显示操作 安装好 OpenCV 之后&#xff0c;首先尝试加载一张最简单的图片并显示出来&#xff0c;代码示例&#xff1a; 第一种方式使用cv2.cv的LoadImage、ShowImage和SaveImage函数 import cv2.cv as cv# 读图片 imagecv.LoadImage(img/image.png, cv.CV_LOA…

C语言fscanf函数了解

fscanf函数从一个流中执行格式化输入,fscanf遇到空格和换行时结束&#xff0c;注意空格时也结束。这与fgets有区别&#xff0c;fgets遇到空格不结束。 原型&#xff1a;int fscanf(FILE *stream, char *format,[argument...]); 返回值&#xff1a;返回实际被转换并赋值的输入项…

linux下gcc编译使用opencv的源文件时报错的处理:undefined reference to symbol '_ZNSsD1Ev@@GLIBCXX_3.4'

这阵子用OpenCV的sift做了一下匹配&#xff0c;在使用gcc编译时遇到这样的一个报错 /usr/bin/ld: /tmp/cceCEx1F.o: undefined reference to symbol _ZNSsD1EvGLIBCXX_3.4 //usr/lib/x86_64-linux-gnu/libstdc.so.6: error adding symbols: DSO missing from command linecoll…

C语言fgets函数了解

原型是&#xff1a;char *fgets(char *s, int n, FILE *stream); 从文件指针stream中读取n-1个字符&#xff0c;存到以s为起始地址的空间里&#xff0c;直到读完一行&#xff0c;如果成功则返回s的指针&#xff0c;否则返回NULL。 例如&#xff1a;一个文件是hello,world&…

PCL PCD文件读写

/* 时隔一年&#xff0c;又回来做双目视觉方向&#xff0c;需要重新启航&#xff0c;加油&#xff01;&#xff01;&#xff01; */ #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> int main() { //----------------…

判断两颗棵二叉树是否相等

int CompTree(TreeNode *tree1, TreeNode *tree2) { bool isTree1Null (tree1 NULL); bool isTree2Null (tree2 NULL); //其中一个为NULL,而另一个不为NULL,肯定不相等 if (isTree1Null ! isTree2Null) return 1; //两个都为NULL,一定相等 if (isTree1Null && isTr…

sdut 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

第一次写BFS的题&#xff0c;从开始一脸懵逼到慢慢分析&#xff0c;期间没有看书没查博客&#xff0c;自己安静的做了四十分钟终于做出来了&#xff0c;满满的成就感&#xff0c;我很水&#xff0c;但是我很努力&#xff0c;Mr杰要努力&#xff01; #include<stdio.h> #i…

最简逆波兰模板

#include<iostream> #include<math.h> using namespace std; double exp() { char a[10]; scanf("%s",a);//注意scanf取入字符串遇到‘\0’停止 switch(a[0]){ case :return exp()exp(); case -:return exp()-exp(); case *:return exp()…

windows 静态IP设置

1.打开cmd&#xff0c;输入ipconfig 记录 IP address 子网掩码 网关 打开 wlan 属性 选中一个网络右键 “属性” 设置 ipv4&#xff0c;选择“使用下面的IP地址” 对应填写 ip地址 子网掩码 网关 tips&#xff1a;DNS建议和网关一样

算术表达式的前缀式、中缀式、后缀式相互转换

中缀表达式&#xff08;中缀记法&#xff09;中缀表达式是一种通用的算术或逻辑公式表示方法&#xff0c;操作符以中缀形式处于操作数的中间。中缀表达式是人们常用的算术表示方法。 虽然人的大脑很容易理解与分析中缀表达式&#xff0c;但对计算机来说中缀表达式却是很复杂的&…

sdut 二叉排序树

二叉排序树 Time Limit: 1000MS Memory Limit: 65536KBSubmit Statistic DiscussProblem Description 二叉排序树的定义是&#xff1a;或者是一棵空树&#xff0c;或者是具有下列性质的二叉树&#xff1a; 若它的左子树不空&#xff0c;则左子树上所有结点的值均小于它的根结点…

“CV_RGB2BGR”: 未声明的标识符

#include "opencv2/imgproc/types_c.h" 添加以上头文件

sdut 图的深度遍历

图的深度遍历Time Limit: 1000MS Memory Limit: 65536KBSubmit Statistic DiscussProblem Description请定一个无向图&#xff0c;顶点编号从0到n-1&#xff0c;用深度优先搜索(DFS)&#xff0c;遍历并输出。遍历时&#xff0c;先遍历节点编号小的。Input输入第一行为整数n&…

windows pytorch环境安装配置

1.下载anaconda windows版本下载地址

赫夫曼编码

http://blog.csdn.net/webzhuce/article/details/53105831

哈佛大学凌晨四点半

哈佛老师经常给学生这样的告诫&#xff1a;如果你想在进入社会后&#xff0c;在任何时候任何场合下都能得心应手并且得到应有的评价&#xff0c;那么你在哈佛的学习期间&#xff0c;就没有晒太阳的时间。作为闻名于世的学府&#xff0c;哈佛大学培养了许多名人&#xff0c;他们…

优先队列c++ STL用法

优先队列(priority queue)普通的队列是一种先进先出的数据结构&#xff0c;元素在队列尾追加&#xff0c;而从队列头删除。在优先队列中&#xff0c;元素被赋予优先级。当访问元素时&#xff0c;具有最高优先级的元素最先删除。优先队列具有最高级先出 &#xff08;first in, l…

window 远程linux

1.我们需要在Linux安装ssh服务 sudo apt-get install openssh-server 2.然后开启ssh服务 sudo /etc/init.d/ssh start 3.在window上安装PUTTY 下载网址 4.填写Linux ip 查看ip: 在Linux输入&#xff1a;ifconfig 5.然后填写登陆Linux的账户名和密码 大功告成。。。。。…

赫夫曼编码长度计算问题?

例题&#xff1a;一组字符(a,b,c,d)在文中出现的次数分别为(7,6,3,5),字符&#xff07;d&#xff07;的哈夫曼编码的长度为&#xff1f; 题解&#xff1a; 首先构造huffman树 每一步都将所有数字排序 方法如下: 1: 3 5 6 7 2: 6 7 8 / \ 3 5 3: 8 13 / \ / \ 3 5 6 7 4: 21 /…