简体   繁体   English

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

[英]copy string one place to another

copy string between comma 在逗号之间复制字符串

input 输入

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

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

i want hold input 我想按住输入

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

You may have to use strtok . 您可能必须使用strtok

Here is the complete solution to all your problems: 这是您所有问题的完整解决方案:

// 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;
}

Test run: 测试运行:

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

Avoid using gets(3) , it lead to some interesting issues even in the early days of the Internet due to easy buffer overflow . 避免使用gets(3) ,即使在Internet初期,由于容易发生缓冲区溢出,也会导致一些有趣的问题 Use the fgets(3) instead. 请改用fgets(3)

If you're sure you're always going to have four inputs, you can use something like: 如果您确定总是要有四个输入,则可以使用类似以下的方法:

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

if you don't know the number of inputs, you'd probably do the reading in a loop instead: 如果您不知道输入数量,则可以循环阅读:

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

or, if you prefer, you could write the loop like this: 或者,如果您愿意,可以这样编写循环:

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

Either way, this reads data that doesn't contain a comma followed by a comma (that is read to verify its presence) until that fails. 无论哪种方式,它都会读取不包含逗号的数据,然后读取逗号以验证其是否存在,直到失败为止。 Assuming the data is in the proper format, that will fail when there's data without a trailing comma. 假设数据采用正确的格式,那么当数据中没有尾随逗号时,它将失败。 We then do one more read after the loop to read the remainder of the line into the last item. 然后,我们在循环后再读一遍,以将行的其余部分读到最后一项。

Note that if your data can also contain a comma, something like: 请注意,如果您的数据还可以包含逗号,则类似于:

(aaa,bbb),(ccc,ddd) (aaa,bbb),(ccc,ddd)

where the first data item should be "(aaa,bbb)" and the second "(ccc,ddd)", this would not work -- for something like that, you could rewrite the conversion for an individual input to something like: "%[^)])," to read up to the closing parenthesis, followed by a parenthesis followed by a comma. 其中第一个数据项应为“(AAA,BBB)”,第二个“(CCC,DDD)”,这是行不通的-类似的东西,你可以为一个单独的输入转换改写为类似:“ %[^)]),”读取到右括号,然后是括号,然后是逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM