简体   繁体   中英

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 .

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 . Use the fgets(3) instead.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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