69,336
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
char *input = NULL;
int i, j, sepCount, inputLength;
/**
* 初始化各种变量
*/
void init()
{
i = 0;
j = 0;
sepCount = 0;
inputLength = 0;
}
/**
* 计算需要的指针数组长度
*
* @param argement 需要分割的字符串
* @param separator 分割符号,比如'/'
*/
void getSepCount(char *argement, char separator)
{
input = argement;
inputLength = strlen(input);
for(i = 0;i < inputLength;i++)
{
if(input[i] == '/')
sepCount++;
}
}
/**
* 分割字符串过程方法
*
* @param separator 分割符号,比如'/'
*/
char ** sepResult(char separator)
{
int array_pointer_length = sepCount + 1;
char **str = (char**)malloc(sizeof(char*) * array_pointer_length);
int pos = 0;
char *p = input;
for(i = 0;i < inputLength;i++)
{
pos++;
if(input[i] == separator)
{
str[j] = (char*)malloc(sizeof(char) * pos);
strncpy(str[j], p, pos - 1);
str[j][pos + 1] = '\0';
p = p + pos;
pos = 0;
j++;
}
}
str[j] = (char*)malloc(sizeof(char) * (pos+1));
strncpy(str[j], p, pos+1);
str[j][pos] = '\0';
return str;
}
/**
* 根据传入的字符串和分割符号进行分割操作,并返回结果
*
* @param str_sep 需要分割的字符串
* @param separator 分割符号,比如'/'
*/
char ** sep_string_result(char *str_sep, char separator)
{
init();
getSepCount(str_sep, separator);
char **str = sepResult(separator);
return str;
}
/**
* 释放各种资源和创建的指针
*
* @param str 创建的用来存储的字符串指针数组
*/
void release(char **str)
{
int length = sepCount + 1;
int i = 0;
for(i = 0; i < length; i++)
{
free(str[i]);
str[i] = NULL;
}
free(str);
str = NULL;
input = NULL;
}
int main(int args, char *argv[], char *env[])
{
char **str = sep_string_result("mytest/webapps/StrutsTest/file/tarfile/123.tar", '/');
int length = sepCount + 1;
for(i = 0; i < length; i++)
printf("%s\n", str[i]);
release(str);
}
#include <stdio.h>
#include <string.h>
unsigned int getlen(char *p,char *t)
{
int len = 1;
char *tp=p;
while((tp=strstr(tp,t))!=0)
{
++len;
tp += 1;
}
tp = p+strlen(p)-1;
if(*tp == *t)
{
--len;
}
return len;
}
int main()
{
char c[]="mytest/webapps/StrutsTest/file/tarfile/123.tar";
unsigned int len = getlen(c,"/");
printf("len:%d\n",len);
return 0;
}
char ** sepResult(char separator)
{
int array_pointer_length = sepCount + 1;
char **str = (char**)malloc(sizeof(char*) * array_pointer_length);
int pos = 0;
char *p = input;
for(i = 0;i < inputLength;i++)
{
pos++;
if(input[i] == separator)
{
str[j] = (char*)malloc(sizeof(char) * pos+1);
memset(str[j],0,pos+1);//这个是我加的,长度有一处我也改了点.
strncpy(str[j], p, pos - 1);
// str[j][pos] = '\0';
p = p + pos;
pos = 0;
j++;
}
}
str[j] = (char*)malloc(sizeof(char) * (pos+1));
strncpy(str[j], p, pos+1);
str[j][pos] = '\0';
return str;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *input = NULL;
int i, j, sepCount, inputLength;
/**
* 初始化各种变量
*/
void init()
{
i = 0;
j = 0;
sepCount = 0;
inputLength = 0;
}
/**
* 计算需要的指针数组长度
*
* @param argement 需要分割的字符串
* @param separator 分割符号,比如'/'
*/
void getSepCount(char *argement, char separator)
{
input = argement;
inputLength = strlen(input);
for(i = 0;i < inputLength;i++)
{
if(input[i] == '/')
sepCount++;
}
}
/**
* 分割字符串过程方法
*
* @param separator 分割符号,比如'/'
*/
char ** sepResult(char separator)
{
int array_pointer_length = sepCount + 1;
char **str = (char**)malloc(sizeof(char*) * array_pointer_length);
int pos = 0;
char *p = input;
for(i = 0;i < inputLength;i++)
{
pos++;
if(input[i] == separator)
{
str[j] = (char*)malloc(sizeof(char) * pos+1);
memset(str[j],0,pos+1);
strncpy(str[j], p, pos - 1);
// str[j][pos] = '\0';
p = p + pos;
pos = 0;
j++;
}
}
str[j] = (char*)malloc(sizeof(char) * (pos+1));
strncpy(str[j], p, pos+1);
str[j][pos] = '\0';
return str;
}
/**
* 根据传入的字符串和分割符号进行分割操作,并返回结果
*
* @param str_sep 需要分割的字符串
* @param separator 分割符号,比如'/'
*/
char ** sep_string_result(char *str_sep, char separator)
{
init();
getSepCount(str_sep, separator);
char **str = sepResult(separator);
return str;
}
/**
* 释放各种资源和创建的指针
*
* @param str 创建的用来存储的字符串指针数组
*/
void release(char **str)
{
int length = sepCount + 1;
int i = 0;
for(i = 0; i < length; i++)
{
free(str[i]);
str[i] = NULL;
}
free(str);
str = NULL;
input = NULL;
}
int main(int args, char *argv[], char *env[])
{
char **str = sep_string_result("mytest/webapps/StrutsTest/file/tarfile/123.tar", '/');
int length = sepCount + 1;
for(i = 0; i < length; i++)
printf("%s\n", str[i]);
release(str);
}