• 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 ", 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# ", 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 = ''; //将p所指的char设成0 printf("%s# ", s); *p = t; //再把原来的p所指的值换回来 p = strstr(p+2, "is"); } return 0; }
  • 相关阅读:
    Delphi stdCall意义
    Delphi 与 DirectX 之 DelphiX(10): TPictureCollectionItem.StretchDraw
    delphi中的TCollection
    Delphi XE5教程8:使用Delphi命名空间
    在 centos 系统中添加审计用户并利用 sudoers 进行权限控制
    在 centos 8 中添加 sudoer 用户
    React.Fragment
    js保留两位小数方法总结
    正则表达式的() [] {} 的区别
    Typora如何配置gitee图床
  • 原文地址:https://www.cnblogs.com/profesor/p/13195971.html
Copyright © 2020-2023  润新知