[英]copy string one place to another
在逗號之間復制字符串
輸入
(aaa),(ddD),(sss),(ppp)
p=malloc(sizeof(char)*200);
gets(p);
我想按住輸入
p[0]="(aaa)"
p[1]="(ddD)"
p[2]="(sss)"
p[3]="(ppp)"
您可能必須使用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)
如果您確定總是要有四個輸入,則可以使用類似以下的方法:
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)”,這是行不通的-類似的東西,你可以為一個單獨的輸入轉換改寫為類似:“ %[^)]),”讀取到右括號,然后是括號,然后是逗號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.