Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Char to String in C

Tags:

c

string

char

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

like image 429
user1911575 Avatar asked Mar 24 '14 22:03

user1911575


People also ask

Can you convert a char to a string?

We can convert a char to a string object in java by using the Character. toString() method.

Can you convert a string to a char in C?

The c_str() and strcpy() function in C++C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0').

How do I turn a char array into a string?

char[] arr = { 'p', 'q', 'r', 's' }; The method valueOf() will convert the entire array into a string. String str = String. valueOf(arr);


1 Answers

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

char str[2] = "\0"; /* gives {\0, \0} */ 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.

like image 191
Adam Avatar answered Oct 04 '22 07:10

Adam