首页 > 编程语言 > 详细

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

时间:2020-09-13 11:44:11      阅读:101      评论:0      收藏:0      [点我收藏+]
#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;
}

  技术分享图片

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

原文:https://www.cnblogs.com/nowroot/p/13660647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!