2010-10-17 74 views
-1

如何将一个字符串分成几部分?例如,我如何放置“orld”。变成一个名为one的变量,“你好”变成了一个叫做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; 
} 
+0

这没有任何意义。 “将问候分成三个单词”意味着什么? – bmargulies 2010-10-17 22:48:38

+0

这应该是一个通用的答案,或只为“Hello World”?你已经澄清了你的问题,因为现在它是模糊的,可能是功课。 – birryree 2010-10-17 22:51:45

+0

为了减少混淆,我编辑了这个问题,但仍然完全符合他原先的意图(密切关注他所调用的字符串,为什么他会有* 3个变量,如果他显然只有两个单词?)。其他编辑改变了问题的含义,答案不会是他想要的。 – 2010-10-17 23:26:16

回答

4

其中之一,你不能做你的问题,仍然有他们作为空终止的字符串工作。这是因为text内存布局是这样的:

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

通知的'\0'底。字符数组test实际上不是长度12,它是长度。

空终止所需的控制台输出函数,如printf和std ::法院(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]; 
如果你想少做typi

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 */ 

您可以通过手来简单地复制值超出做到这一点NG,或只是想写出更好的代码,你可以利用一个事实,即数组/串是连续的优势,并使用一个循环来将数据复制过来:

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 */ 
} 
2

首先,你需要从char改变onetwo,并且three到字符指针或字符数组 - 一个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是否返回有效(非空)指针。

+0

这是一个干净的答案,但是如果他在这个问题上有问题,那么他很可能在动态分配内存时遇到更多问题。尽管如此,我仍然投票赞成。 – 2010-10-17 23:29:49

+0

你将会在你的'malloc()'行出现编译错误。它需要被转换为char *' – 2015-06-04 20:40:27

+0

@ b1nary.atr0phy:不,它不需要(也不应该)。如果您将它编译为C++,则只需要进行转换(但如果您正在编写C++,则通常不应该使用'malloc')。 – 2015-06-04 20:43:47