把一行字符串分隔然后取出每组

baidingling 2011-05-17 12:03:53
这行字符串比如是test,有空格也可能有tab键,只要有空格或者tab都要分隔开,然后把分隔开的每组字符串放到p[]数组中。代码怎么写呢,谢谢大家!
...全文
129 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidingling 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhao4zhong1 的回复:]
C/C++ code
//这行字符串比如是test,有空格也可能有tab键,只要有空格或者tab都要分隔开,然后把分隔开的每组字符串放到p[]数组中。
#include <stdio.h>
#include <string.h>
char test[4096];
char p[100][21];//最多保存100个token,每个token不超过20个字符
int n,i;
char……
[/Quote]

完整准确!谢谢!
baidingling 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dizuo 的回复:]
C/C++ code
#include <stdio.h>
#include <string.h>

int main(void)
{
char headinfo[4096];
……
[/Quote]

谢谢,就是没看我题目写的分隔符是啥~
baidingling 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hnuqinhuan 的回复:]
C/C++ code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char str[1000];
char ss[50][50];
gets(str);
int j = 0,k = 0;
unsigned int i = 0;
……
[/Quote]
对于字符串中含有多个空格和tab就不行了这个。。。
ryfdizuo 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 baidingling 的回复:]

引用 2 楼 last_c_ 的回复:
用strtok这个函数就可以了


想过这个函数,能实现? 能否给出代码,谢谢~~~
[/Quote]
#include <stdio.h>
#include <string.h>

int main(void)
{
char headinfo[4096];
char seps1[] = ";";
char seps2[] = ",;";
char *token1;
char *token2;

strcpy(headinfo, "field1,100,0;field2,80,1;field3,120,2");

token1 = strtok(headinfo, seps1);
while (token1 != NULL)
{
/* While there are tokens in headinfo */
printf("%s\n", token1);
/* Get next token: */
token1 = strtok(NULL, seps1);
}

strcpy(headinfo, "field1,100,0;field2,80,1;field3,120,2"); /*strtok modofy headinfo, so recopy*/
token2 = strtok(headinfo, seps2);
while (token2 != NULL)
{
/* While there are tokens in iteminfo */
printf("%s\n", token2);
/* Get next token: */
token2 = strtok(NULL, seps2);
}

return 0;
}
無_1024 2011-05-17
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char str[1000];
char ss[50][50];
gets(str);
int j = 0,k = 0;
unsigned int i = 0;
for( i = 0;i < strlen(str); ++i )
{
if( str[i] == ' ' || str[i] == '\t' || str[i] == '\b' )
{
ss[k][j] = '\0';
j = 0;
k++;
}
else
{
ss[k][j++] = str[i];
}
}
ss[k][j] = '\0';
for( int m = 0; m <= k; ++m )
{
printf("%s\n",ss[m]);
}
return 0;
}
baidingling 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 last_c_ 的回复:]
用strtok这个函数就可以了
[/Quote]

想过这个函数,能实现? 能否给出代码,谢谢~~~
baidingling 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 namelij 的回复:]
两个标记,第一个标记用来表示第一个不是空格或者tab的字符,然后第二个标记移动,如果找到空格或者tab,则用memcpy将两个标记之间的内容拷贝到你的数组中

然后第一个标记移动到 上面的 第二个标记 的下一个字节处...重新进行检索
[/Quote]

能否给出代码。。。谢谢~~~
LaSt_C_ 2011-05-17
  • 打赏
  • 举报
回复
用strtok这个函数就可以了
  • 打赏
  • 举报
回复
两个标记,第一个标记用来表示第一个不是空格或者tab的字符,然后第二个标记移动,如果找到空格或者tab,则用memcpy将两个标记之间的内容拷贝到你的数组中

然后第一个标记移动到 上面的 第二个标记 的下一个字节处...重新进行检索
pathuang68 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhao4zhong1 的回复:]

C/C++ code
//这行字符串比如是test,有空格也可能有tab键,只要有空格或者tab都要分隔开,然后把分隔开的每组字符串放到p[]数组中。
#include <stdio.h>
#include <string.h>
char test[4096];
char p[100][21];//最多保存100个token,每个token不超过20个字符
int n,i;
char seps……
[/Quote]

++
可乐崽 2011-05-17
  • 打赏
  • 举报
回复
参考,可以把参数改为你要的字符!
/*
* split a string into words, must free after use
*/
int str_split(char ***words, const char *line, char delim) {
int word_counter = str_count_char(line, delim) + 1;
*words = (char **) malloc(sizeof(char *) * word_counter);
int i = 0;
char *p = line;
while ( 1 ) {
char *s = p;
p = strchr(p, delim);
int len = ( p ) ? p - s : strlen(s);
(*words)[i] = (char *) malloc(sizeof(char) * (len + 1));
strncpy((*words)[i], s, len);
(*words)[i][len] = 0;
if ( !p ) break;
p++;
i++;
}
return word_counter;
}

void test_str_split(){
printf("\n\n<%d>str_split() test:\n", Counter ++);
char **words;
char line[] = "Hello! How are you? Fine. Thank you!";
char delim = ' ';

int i = 0, j = 0;
int word_counter = str_split(&words, line, delim);
printf(" line: %s", line);
for(i=0; i<word_counter; i++){
printf("\n words[%d]: ",i);
j = 0;
while(words[i][j] != '\0'){
printf("%c", words[i][j++]);
}
free(words[i]);
}
free(words);
}

赵4老师 2011-05-17
  • 打赏
  • 举报
回复
上面代码中
for (i=0;i<1;i++) {
应改为
for (i=0;i<n;i++) {
赵4老师 2011-05-17
  • 打赏
  • 举报
回复
//这行字符串比如是test,有空格也可能有tab键,只要有空格或者tab都要分隔开,然后把分隔开的每组字符串放到p[]数组中。
#include <stdio.h>
#include <string.h>
char test[4096];
char p[100][21];//最多保存100个token,每个token不超过20个字符
int n,i;
char seps[] = " \t";
char *token;
int main(void)
{

strcpy(test, "field1234567890123456789 100\t0 field2\t\t80\t \t1 field3 120 2");

n=0;
token = strtok(test, seps);
while (token != NULL)
{
printf("%s\n", token);
strncpy(p[n],token,20);p[n][20]=0;//忽略20个以后的字符
n++;
if (n>=100) break;//忽略100个以后的token
token = strtok(NULL, seps);
}
for (i=0;i<1;i++) {
printf("%d [%s]\n",i+1,p[i]);
}

return 0;
}

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧