strstr函数的使用

strstr函数的使用例://找出字符串中所有的is//找出字符串中所有的is#include<stdio.h>#include<string.h>intmain(intargc,charconst*argv[]){chars[200]="Workis

大家好,欢迎来到IT知识分享网。strstr函数的使用"

例://找出字符串中所有的is
//找出字符串中所有的is
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    char s[200] = "Work is like a capricious lover whose incessant demands are resented but who is missed terribly when she is not there."; 
// To survive in the new world of retail shopkeepers need large amounts of imagination and money."; char *p = strstr(s, "is"); //如果找到"is",返回'i'所在的地址 while (p) { printf("%s\n", p); p = strstr(p+2, "is"); //指针向下移,用p+1, p+2都行, } return 0; }

 

//找出字符串中所有的is,且只输出”is”:

//找出字符串中所有的is,且只输出"is":
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    char s[200] = "Work is like a capricious lover whose incessant demands are resented but who is missed terribly when she is not there."; 
// To survive in the new world of retail shopkeepers need large amount of imagination and money."; char *p = strstr(s, "is"); //如果找到"is",返回'i'所在的地址 while (p) { char t = *(p+2); *(p+2) = 0; printf("%s#\n", p); *(p+2) = t; p = strstr(p+2, "is"); } return 0; }

 

 

//找出字符串中所有的is,且只输出”is”前面的内容:

//找出字符串中所有的is,且只输出"is"前面的内容:
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    char s[200] = "Work is like a capricious lover whose incessant demands are resented but who is missed terribly when she is not there."; 
// To survive in the new world of retail shopkeepers need large amount of imagination and money."; char *p = strstr(s, "is"); //如果找到"is",返回'i'所在的地址 while (p) { char t = *p; //临时存放下p所指的char *p = '\0'; //将p所指的char设成0 printf("%s#\n", s); *p = t; //再把原来的p所指的值换回来 p = strstr(p+2, "is"); } return 0; }

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/30905.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信