zoukankan      html  css  js  c++  java
  • 字符串截取函数-c语言

    1 #include<stdio.h>
    2 #include<stdlib.h>
    3
    4 char* substring(char* ch,int pos,int length) 
    5
    6     char* pch=ch; 
    7 //定义一个字符指针,指向传递进来的ch地址。 
    8     char* subch=(char*)calloc(sizeof(char),length+1); 
    9 //通过calloc来分配一个length长度的字符数组,返回的是字符指针。 
    10     int i; 
    11 //只有在C99下for循环中才可以声明变量,这里写在外面,提高兼容性。 
    12     pch=pch+pos; 
    13 //是pch指针指向pos位置。 
    14     for(i=0;i<length;i++
    15     { 
    16         subch[i]=*(pch++); 
    17 //循环遍历赋值数组。 
    18     } 
    19     subch[length]='';//加上字符串结束符。 
    20     return subch;       //返回分配的字符数组地址。 
    21 }
    22
    23 int main(){
    24     char* result;
    25     char* tstStr = "abcdefg";
    26     result = substring(tstStr,0,2);
    27    
    28     printf("结果:%s",result);
    29    
    30 }

  • 相关阅读:
    解决调用未定义 swoole_async_readfile函数问题
    7000字 Redis 超详细总结笔记总 | 收藏必备!
    C/C++语言编程修养
    glib 队列
    sprintf 详解
    json 需替换 特殊字符串
    glib 关系
    glib 简介
    gprof 代码效率测量
    glib 树
  • 原文地址:https://www.cnblogs.com/dpf-learn/p/6257396.html
Copyright © 2011-2022 走看看