如何在任意索引处拆分字符串?

时间:2010-10-17 22:35:42

标签: c string split

如何将字符串拆分成碎片?例如,我如何放置“orld”。变成名为one的变量,将“Hello”变成名为three的变量,将“w”变成two

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

int main(void)
{
    char *text ="Hello World."; /*12 C*/

    char one[5];
    char two[5];
    char three[2];

    return 1;
}

2 个答案:

答案 0 :(得分:4)

首先,你不能做你所要求的,仍然让它们作为以空字符结尾的字符串。这是因为text的内存布局如下所示:

char text_as_array[] = {
  'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '.', '\0'
};

注意最后的'\0'。字符数组test实际上不是长度12,长度 13

控制台输出函数需要空终止,例如printf和std :: cout(C ++)才能工作:

char good[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
printf("%s\n", good); /* this works, because it is null-terminated */

char bad[] = { 'H', 'e', 'l', 'l', };
printf("%s\n", bad); /* this will print out garbage, or crash your program! */

这意味着您必须像这样定义数组:

char one[6];
char two[3];
char three[6];

您可以通过简单地复制值来手动执行此操作:

one[0] = text[7]; /* o */
one[1] = text[8]; /* r */
one[2] = text[9]; /* l */
one[3] = text[10]; /* d */
one[4] = text[11]; /* . */
one[5] = '\0'; /* you have to null-terminate the string */

如果你想减少输入,或者只是想编写更好的代码,你可以利用数组/字符串是连续的这一事实,并使用循环来复制数据:

for(int i = 0; i < 5; ++i)
{
  one[i] = text[7 + i];
}
one[5] = '\0';

但是如果你猜这在C中真的很常见,那么你猜对了。您不应每次都手动编码此循环,而应使用内置函数为您进行复制:

/* C uses "", C++ uses <> */
#include "string.h" /* C++: #include<cstring> */
#include "stdio.h" /* C++: #include<cstdio> */

/* don't do "int function(void)", do "int function()" instead */
int main()
{
  char *text = "Hello World."; /* string length 12, array size 13 */

  /* size of these has to accomodate the terminating null */
  char one[6];
  char two[3];
  char three[6];

  /* copy two characters, starting at index 5 */
  strncpy(two, &text[5], 2);

  /* for three, we don't have to do &text[0], because it is at the beginning of the string */
  strncpy(three, text, 5);

  /* we can do strcpy if we're at the end of the string.  It will stop when it hits '\0' */
  strcpy(one, &text[7]);

  /* note that we still have to null-terminate the strings when we use strncpy */
  two[2] = '\0';
  three[5] = '\0';

  /* but we don't have to null-terminate when we use strcpy */
  /* so we can comment this out: one[5] = '\0'; */

  printf("%s\n", one);
  printf("%s\n", two);
  printf("%s\n", three);

  return 0; /* returns 0, since no errors occurred */
}

答案 1 :(得分:2)

首先,您需要将onetwothreechar更改为指向char或char数组的指针 - char成立只有一个字符,而不是一串字符。

假设您使用指向char的指针,您可以执行以下操作:

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

char *substr(char const *input, size_t start, size_t len) { 
    char *ret = malloc(len+1);
    memcpy(ret, input+start, len);
    ret[len]  = '\0';
    return ret;
}

int main() { 
    char *text = "Hello World.";
    char *one = substr(text, 7, 4);
    char *two = substr(text, 5, 2);
    char *three = substr(text, 0, 5);

    printf("\"%s\"\t\"%s\"\t\"%s\"\n", one, two, three);

    free(one);
    free(two);
    free(three);

    return 0;
}

请注意,这会动态分配子字符串的内存。调用者可以在不再需要时释放字符串。另请注意,为了简单和清晰起见,我省略了错误检查。对于实际代码,您需要检查malloc在复制数据之前返回有效(非空)指针。