首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在任意索引处拆分字符串?

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

Stack Overflow用户
提问于 2010-10-18 06:35:42
回答 2查看 14.3K关注 0票数 0

我怎样才能把一根线拆成几段呢?例如,如何放置“orld”。添加到名为one的变量中,将"Hello“添加到名为three的变量中,将”w“添加到two

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-18 07:09:52

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

代码语言:javascript
复制
char text_as_array[] = {
  'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '.', '\0'
};

注意末尾的'\0'。字符数组test的实际长度不是12,而是13。

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

代码语言:javascript
复制
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! */

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

代码语言:javascript
复制
char one[6];
char two[3];
char three[6];

您可以通过简单地复制这些值来手动完成此操作:

代码语言:javascript
复制
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 */

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

代码语言:javascript
复制
for(int i = 0; i < 5; ++i)
{
  one[i] = text[7 + i];
}
one[5] = '\0';

但是如果你猜到这是在C中很常见的事情,那么你猜对了。您应该使用内置函数为您执行复制操作,而不是每次手动编写此循环的代码:

代码语言:javascript
复制
/* 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 */
}
票数 4
EN

Stack Overflow用户

发布于 2010-10-18 06:58:47

首先,需要将onetwothreechar改为指向字符或字符数组的指针-- char只包含单个字符,而不是它们的字符串。

假设您使用指向char的指针,您可以这样做:

代码语言:javascript
复制
#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是否返回了有效的(非空)指针。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3955601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档