当前位置: 代码迷 >> C语言 >> 关于字符串的处理问题
  详细解决方案

关于字符串的处理问题

热度:268   发布时间:2008-05-29 20:57:25.0
关于字符串的处理问题
给出一个字符串,比如char *string = "/mnt/flash/test/";
返回一个新的串,char *str ="/mnt/flash/";
也就是处理一次返回上一级目录

自己写了一个,但是运行时偶尔会出错,大家帮忙改改


char *getnewstring(char *str)
{
    int len = strlen(str);
    char *tmp = (char *)malloc(len);

    memcpy(tmp, str, len-1);
    char *p,*q;
    p = strpbrk(tmp, "/");
    q = strrchr(tmp, '/');
    char *tmp1 = (char *)malloc((q-p+1)*sizeof(char));
    memcpy(tmp1, tmp, q-p+1);

    return tmp1;

}
搜索更多相关的解决方案: char  tmp  字符  str  len  

----------------解决方案--------------------------------------------------------
  相关解决方案