C语言 C 中将字符转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22621952/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:55:24  来源:igfitidea点击:

Convert Char to String in C

cstringchar

提问by user1911575

How do I convert a character to a string in C. I'm currently using c = fgetc(fp)which returns a character. But I need a string to be used in strcpy

如何在 C 中将字符转换为字符串。我目前正在使用c = fgetc(fp)它返回一个字符。但是我需要在 strcpy 中使用一个字符串

回答by Adam

To answer the question without reading too much else into it i would

要回答这个问题而不阅读太多其他内容,我会

char str[2] = "
char buffer[MAX_SIZE_OF_MY_BUFFER];

int i = 0;
char ch;
while (i < MAX_SIZE_OF_MY_BUFFER - 1 && (ch = fgetc(fp)) != EOF) {
    buffer[i++] = ch;
}
buffer[i] = '
...
    strcpy( ... , (char[2]) { (char) c, '
char c = 'A';
char str1[2] = {c , '
int i = 0;
char string[256], c;
while(i < 256 - 1 && (c = fgetc(fp) != EOF)) //Keep space for the final 
//example
char character;//to be scanned
char merge[2];// this is just temporary array to merge with      
merge[0] = character;
merge[1] = '
char c = std::fgetc(fp);
std::strcpy(buffer, &c);
'; //now you have changed it into a string
{ string[i++] = c; } string[i] = '
strcpy(char* dest, const char* src);
';
'}; char str2[5] = ""; strcpy(str2,str1);
' } ); ...
'; // terminating character
"; /* gives {
printf("-%s-", (char[2]){'A', 0});
,
char c[10];
int i=0;
while(i!=10)
{
    c[i]=fgetc(fp);
    i++;
}
} */ str[0] = fgetc(fp);

You could use the second line in a loop with what ever other string operations you want to keep using char's as strings.

您可以在循环中使用第二行以及您希望继续使用 char 作为字符串的其他字符串操作。

回答by LihO

Using fgetc(fp)only to be able to call strcpy(buffer,c);doesn't seem right.

使用fgetc(fp)只能够调用strcpy(buffer,c);看起来不正确。

You could simply build this buffer on your own:

您可以简单地自行构建此缓冲区:

##代码##

Note that this relies on the fact that you will read less than MAX_SIZE_OF_MY_BUFFERcharacters

请注意,这取决于您将阅读少于MAX_SIZE_OF_MY_BUFFER字符的事实

回答by ThoAppelsin

You could do many of the given answers, but if you just want to do it to be able to use it with strcpy, then you could do the following:

您可以做许多给定的答案,但如果您只是想这样做以便能够将其与 一起使用strcpy,那么您可以执行以下操作:

##代码##

The (char[2]) { (char) c, '\0' }part will temporarily generate null-terminated string out of a character c.

(char[2]) { (char) c, '\0' }部分将从字符中临时生成以空字符结尾的字符串c

This way you could avoid creating new variables for something that you already have in your hands, provided that you'll only need that single-character stringjust once.

这样您就可以避免为您已经拥有的东西创建新变量,前提是您只需要一次该单字符字符串

回答by ?? ???fik

I use this to convert char to string (an example) :

我用它来将字符转换为字符串(一个例子):

##代码##

回答by Taiki

A code like that should work:

这样的代码应该可以工作:

##代码##

回答by Omar M. Hussein

##代码##

回答by Floriel

This is an old question, but I'd say none of the answers really fits the OP's question. All he wanted/needed to do is this:

这是一个老问题,但我想说没有一个答案真正适合 OP 的问题。他想要/需要做的就是:

##代码##

The relevant aspect here is the fact, that the second argument of strcpy()doesn't need to be a char array / c-string. In fact, none of the arguments is a char or char array at all. They are both char pointers:

这里的相关方面是,第二个参数strcpy()不需要是字符数组/c 字符串。事实上,没有一个参数是 char 或 char 数组。它们都是字符指针:

##代码##

dest :A non-const char pointer
Its value has to be the memory address of an element of a writable char array (with at least one more element after that).
src :A const char pointer
Its value can be the address of a single char, or of an element in a char array. That array must contain the special character \0within its remaining elements (starting with src), to mark the end of the c-string that should be copied.
dest :非常量字符指针
它的值必须是可写字符数组元素的内存地址(之后至少还有一个元素)。
src :一个 const 字符指针
它的值可以是单个字符的地址,也可以是字符数组中元素的地址。该数组必须\0在其剩余元素中包含特殊字符(以 开头src),以标记应复制的 c 字符串的结尾。



回答by John

Here is a working exemple :

这是一个工作示例:

##代码##

This will display -A-

这将显示 -A-

回答by Dhananjayan Santhanakrishnan

FYI you dont have string datatype in C. Use array of characters to store the value and manipulate it. Change your variable c into an array of characters and use it inside a loop to get values.

仅供参考,您在 C 中没有字符串数据类型。使用字符数组来存储值并对其进行操作。将变量 c 更改为字符数组,并在循环中使用它来获取值。

##代码##

The other way to do is to use pointers and allocate memory dynamically and assign values.

另一种方法是使用指针并动态分配内存并赋值。