将字符串复制到另一个地方

时间:2010-05-19 11:52:02

标签: c

复制逗号之间的字符串

输入

(aaa),(ddD),(sss),(ppp)

p=malloc(sizeof(char)*200);
gets(p);

我想要保留输入

p[0]="(aaa)"
p[1]="(ddD)"
p[2]="(sss)"
p[3]="(ppp)"

3 个答案:

答案 0 :(得分:4)

您可能必须使用strtok

以下是解决所有问题的完整解决方案:

// tokens.c

#include <stdio.h>
#include <string.h> /* for strtok, strlen and strcpy. */
#include <stdlib.h> /* for malloc, realloc and free. */ 

static char **tokens = NULL; /* Dynamic array of string tokens. */
static int token_count = 0; /* Number of tokens added. */

/* Grows the `tokens' array as needed and appends `tok' to it. */
static void
copy_token (char *tok)
{
  if (token_count == 0)
    tokens = malloc (sizeof (char*));
  else
    tokens = realloc (tokens, sizeof (char*) * (token_count + 1));
  tokens[token_count] = malloc (strlen (tok) + 1);
  strcpy (tokens[token_count], tok);
  ++token_count;
}

/* Extracts tokens from `s' and calls copy_token to add it to `tokens'. */
static void
tokenize_by_comma (char *s)
{
  char *tok = strtok (s, ",");
  while (tok != NULL)
    {      
      copy_token (tok);
      tok = strtok (NULL, ",");
    }  
}

/* If you run copy_after, the total length of all tokens 
   must not exceed BUFF_SIZE. */
#define BUFF_SIZE 1024 
static char s_copy[BUFF_SIZE + 1];

/* Makes a string of all the tokens by moving `s' next to `after'. */
static char *
copy_after (const char *s, const char *after)
{
  int i;
  int appended = 0;
  strcpy (s_copy, "");
  for (i = 0; i < token_count; ++i)
    {
      int is_s = (strcmp (tokens[i], s) == 0);
      int is_after = (strcmp (tokens[i], after) == 0);
      if (is_after)
        {
          strcat (s_copy, after);
          strcat (s_copy, ",");
          strcat (s_copy, s);
          appended = 1;
        }
      else if (!is_s)
        {
          strcat (s_copy, tokens[i]);
          appended = 1;
        }
      if (i != (token_count - 1) && appended) 
        strcat (s_copy, ",");
      appended = 0;
    }
  return s_copy;
}

/* Prints the `tokens'. */
static void 
print_tokens ()
{
  int i;
  for (i = 0; i < token_count; ++i)
    printf ("%s\n", tokens[i]);
}

/* Frees the memory allocated for `tokens'. */
static void 
free_tokens ()
{
  int i;
  for (i = 0; i < token_count; ++i)
    free (tokens[i]);
  free (tokens);
  token_count = 0;
  tokens = NULL;
}

/* Test. Pass the tokens as a single command line argument. */
int
main (int argc, char **argv)
{
  tokenize_by_comma (argv[1]);
  print_tokens ();
  if (argc == 4)
    {
      printf ("%s\n", copy_after (argv[2], argv[3]));
    }
  free_tokens ();
  return 0;
}

试运行:

$ ./tokens "(aaa),(ddD),(sss),(ppp)"
(aaa)
(ddD)
(sss)
(ppp)
$ ./tokens "(aaa),(ddD),(sss),(ppp)" "(ddD)" "(ppp)"
(aaa)
(ddD)
(sss)
(ppp)
(aaa),(sss),(ppp),(ddD)

答案 1 :(得分:1)

避免使用gets(3),即使在互联网发展初期,由于简单interesting issues,也会导致buffer overflow。请改用fgets(3)

答案 2 :(得分:0)

如果你确定你总是会有四个输入,你可以使用类似的东西:

scanf("%[^,],%[^,],%[^,],%[^,]", p[0], p[1], p[2], p[3]);

如果您不知道输入的数量,您可能会在循环中进行读取:

for (i=0; i<limit; i++)
    if (!scanf("%[^,],", p[i]))
        break;
if (i<limit)
    scanf("%[^\n]", p[i]);

或者,如果您愿意,可以像这样编写循环:

for (i=0; i<limit && scanf("%[^,],", p[i]); i++)
    ;

无论哪种方式,这都会读取不包含逗号后跟逗号(读取以验证其存在)的数据,直到失败为止。假设数据格式正确,那么当没有尾随逗号的数据时,这将失败。然后我们在循环之后再读一遍,将行的剩余部分读入最后一项。

请注意,如果您的数据也可以包含逗号,例如:

(AAA,BBB),(CCC,DDD)

其中第一个数据项应为“(aaa,bbb)”,第二个“(ccc,ddd)”,这将工作 - 对于类似的东西,你可以重写将单个输入转换为类似:“%[^]]),”读取右括号,后跟括号后跟逗号。