C语言 如何将三个字符合二为一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16395417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to combine three char to one
提问by user2321372
I had three char inside the array , which contain X1 X2 and X3
我在数组中有三个字符,其中包含 X1 X2 和 X3
char array[3]={X1,X2,X3}
I want to combine three data which inside the array to one char
我想将数组中的三个数据组合成一个字符
strcat(array[0]," ");
strcat(array[0],array[1]);
strcat(array[0]," ");
strcat(array[0],array[2]);
printf("%s",array[0])
I expect to get the result like this "X1 X2 X3"
我希望得到这样的结果“X1 X2 X3”
回答by Mohamad Ali Baydoun
I think you mean you want to concatenate 3 chars to a make one string.
我想你的意思是你想将 3 个字符连接成一个字符串。
Use sprintfwith a sufficient array:
使用sprintf具有足够的数组:
char str[6];
char array[3] = {'l', 'o', 'l'};
// write
sprintf(str, "%c %c %c", array[0], array[1], array[2]);
// print
printf(str);
回答by Sudhee
you will need one more byte to have a null terminator.
您将需要多一个字节才能拥有空终止符。
char array[4] = { 'a', 'b', 'c', 'void chars2str(char x1, char x2, char x3, char* dest) {
sprintf(dest, "%c %c %c", x1, x2, x3);
}
' };
printf( "%s\n", array );
will give your abc
会给你的 abc
回答by Fabien
You can't do it that way. arrayis only 3 bytes long, so you can't have the string "a b c", which is 5+1 = 6 bytes long in it. And, of course, you can't call strcat(array[0], ...since array[0]is a single char, not a char *.
你不能那样做。array只有 3 个字节长,所以你不能有字符串“ab c”,它是 5+1 = 6 个字节长。而且,当然,您不能调用,strcat(array[0], ...因为array[0]它是单个字符,而不是char *.
I'm not sure what you are looking for, but if you are given X1, X2 and X3 and want them in a string separated by spaces, you can do that :
我不确定你在找什么,但如果你得到 X1、X2 和 X3 并希望它们在用空格分隔的字符串中,你可以这样做:
char mystr[6];
char2str('a', 'b', 'c', mystr); /* Now mystr contains "a b c" */
Of course, the destargument must be big enough or something very nasty might happen.
当然,dest论点必须足够大,否则可能会发生非常讨厌的事情。
#include <stdio.h>
char *join(char *result, const char array[], size_t size, const char sep){
size_t i,len=0;
for(i=0;i<size;++i){
len+=sprintf(result+len, "%c", array[i]);
if(i < size - 1)
len+=sprintf(result+len, "%c", sep);
}
return result;
}
int main(void){
const char array[] = { 'x', 'y', 'z'};
char result[6];
join(result, array, sizeof(array)/sizeof(char), ' ');
printf("%s", result);//"x y z"
return 0;
}
回答by Varad
The way you're using strcatis incorrect. array[0]is a char, which means it can hold only a single letter. It cannot hold X1 along witha " " or X2. Create another sufficiently big char array and then you can strcatto that.
你使用的strcat方式不对。array[0]is a char,这意味着它只能容纳一个字母。它不能将 X1与“”或 X2 放在一起。创建另一个足够大的字符数组,然后你就strcat可以了。
回答by BLUEPIXY
char c = 'c';
char d = 'd';
string s1(1, c);
string s2(1, c);
string cd = s1 + s2;
回答by Spencer Goff
You could convert each individual char to a string, then concatenate them.
您可以将每个单独的字符转换为字符串,然后将它们连接起来。
e.g.
例如
##代码##
