C语言strchr使用之Next查找和截断想要的字符串

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>


static char pstring[] =  "Hello\n";   // 这里不能是char*

int main()
{
    char tmp ;
    char* pstr = strchr(pstring,'l');
    printf("pstr is %s %p\n",pstr,pstr);

    #if 0
    // 搜后面的
    pstr = strchr(pstr+1,'l'); // 是strchr不是strstr
    printf("next pastr is %s\n",pstr);
    #endif //

    //需要He
    tmp = *pstr;
    printf("%c",tmp);
    pstr[0] = '\0';
    printf("length is %d\n",strlen(pstring)+1);
    char* data = (char*)malloc(strlen(pstring)+1);
    if(data == NULL)
    {
      printf("malloc failure\n");
      return -1;
    }
    printf("length is %d\n",strlen(pstring)+1);
    strcpy(data,pstring);
    printf("data is %s\n",data);
    *pstr = tmp;
    free(data);

    while(1);
    return 0;
}

  

posted @ 2020-09-13 11:04  卷哭你  阅读(340)  评论(0编辑  收藏  举报