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
We can convert a char to a string object in java by using the Character. toString() method.
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').
char[] arr = { 'p', 'q', 'r', 's' }; The method valueOf() will convert the entire array into a string. String str = String. valueOf(arr);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With