16,472
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int SplitString(const char* str_dest);
int main(int argc, char** argv)
{
char *sz_str = (char*)malloc(100);
memset(sz_str, 0, 100);
while(printf("Wait input:\n"), scanf("%s", sz_str))
{
printf("Result:\n");
SplitString(sz_str);
}
}
int SplitString(const char* str_dest)
{
char sz_temp[100];
char sz_short[10][100];
int n_index = 0;
int n_szCount = strlen(str_dest);
memset(sz_temp, 0, 100);
memset(sz_short, 0, 100 * 10);
assert(n_szCount <= 100);
strcpy(sz_temp, str_dest);
for (int i = 0; i <= 99; i++)
{
for (int n = 0; n <= 100; n++)
{
if (',' != sz_temp[i])
{
sz_short[n_index][n] = sz_temp[i];
i++;
}
else
{
assert( n + 1 <= 100);
sz_short[n_index][n + 1] = '\0';
n_index++;
break;
}
}
}
for (int i = 0; i <= 9; i++)
{
printf("\t%s\n", sz_short[i]);
}
return 0;
}
for (int i = 0; i <= 99; i++)
{
for (int n = 0; n <= 100; n++)
{
if (',' != sz_temp[i])
{
sz_short[n_index][n] = sz_temp[i];
i++;
}
else
{
assert( n + 1 <= 100);
sz_short[n_index][n + 1] = '\0';
n_index++;
break;
}
}
}
//输出部分也应该改一下
for (int i = 0; i < split_count; i++)
{
printf("%s\n", split_flag[i]);
}
int SplitString(const char* str_dest)
{
char sz_temp[100];
char *split_flag[100] = {0};//记录拆分后每个子串的开头地址
split_flag[0] = sz_temp;
int split_count = 1;//记录拆分后子串的个数
int n_szCount = strlen(str_dest);
memset(sz_temp, 0, 100);
memset(sz_short, 0, 100 * 10);
assert(n_szCount <= 100);
strcpy(sz_temp, str_dest);
for (int i = 0; i <= n_szCount && sz_temp[i] != '\0'; i++)
{
if (sz_temp[i] == ',')
{
sz_temp[i] = '\0';
split_flag[split_count++] = (&sz_temp[i]) + 1;
}
}
for (int i = 0; i <= 9; i++)
{
printf("\t%s\n", sz_short[i]);
}
return 0;
}