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都⾏,
}
return0;
}
//出字符串中所有的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;
字符串函数strip的作用p = strstr(p+2, "is");
}
return0;
}
//出字符串中所有的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");
}
return0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。