#include <stdio.h>
#include <stdlib.h>
int main(){
int a[] = {0,0,1,0,0,1,0,2};
const char *table = "0123456789";
size_t size = sizeof(a)/sizeof(*a);
char *b = malloc(size+1);
int i;
for(i=0;i<size;++i)
b[i]=table[a[i]];
b[i]='\0';
printf("%s\n", b);
free(b);
return 0;
}
Answer from BLUEPIXY on Stack Overflowc - How to convert a list to a string - Stack Overflow
.net - Convert a list to a string in C# - Stack Overflow
C# List<string> to string with delimiter - Stack Overflow
Convert string to list
#include <stdio.h>
#include <stdlib.h>
int main(){
int a[] = {0,0,1,0,0,1,0,2};
const char *table = "0123456789";
size_t size = sizeof(a)/sizeof(*a);
char *b = malloc(size+1);
int i;
for(i=0;i<size;++i)
b[i]=table[a[i]];
b[i]='\0';
printf("%s\n", b);
free(b);
return 0;
}
int a = [0,0,1,0,0,1,0,2]
That is not valid C. Perhaps you meant:
const int a[] = { 0, 0, 1, 0, 0, 1, 0, 2 };
Converting a decimal digit to a printable character in C is easy, just add '0':
printf("%c", '0' + a[0]);
will print 0.
Maybe you are trying to do:
string combinedString = string.Join( ",", myList.ToArray() );
You can replace , with whatever character you want to split the elements in the list by.
Also, as mentioned in the comments you could also do:
string combinedString = string.Join( ",", myList);
Reference:
Join<T>(String, IEnumerable<T>)Concatenates the members of a constructed IEnumerable collection of type String, using the specified separator between each member.
I am going to go with my gut feeling and assume you want to concatenate the result of calling ToString on each element of the list.
var result = string.Join(",", list.ToArray());
You can use String.Join. If you have a List<string> then you can call ToArray first:
List<string> names = new List<string>() { "John", "Anna", "Monica" };
var result = String.Join(", ", names.ToArray());
In .NET 4 you don't need the ToArray anymore, since there is an overload of String.Join that takes an IEnumerable<string>.
In newer versions of .NET different String.Join overloads use different approaches to produce the result. And this might affect the performance of your code.
For example, those that accept IEnumerable use StringBuilder under the hood. And the one that accepts an array uses a heavily optimized implementation with arrays and pointers.
Results:
John, Anna, Monica
You can also do this with linq if you'd like
var names = new List<string>() { "John", "Anna", "Monica" };
var joinedNames = names.Aggregate((a, b) => a + ", " + b);
Although I prefer the non-linq syntax in Quartermeister's answer and I think Aggregate might perform slower (probably more string concatenation operations).
If I have a list of char like (cons #\C (cons #\O (cons #\M (cons #\P (cons #\U (cons #\T (cons #\E empty))))))), how can I convert it into a string? I'm not allowed to use string-append, substring, implode, and explode. I am also on Beginning Student so I can't use string-join. I've thought about using (string char) but I can't figure out how it'd work recursively.
Or, how would I be able to convert a list of strings into a single string, given the same restrictions above?
I also posted this on stackoverflow: https://stackoverflow.com/q/69430012/16903929
Googling it just gave me info on converting a character array into a string. The method might be the same but putting down convertToString gives me an error. Do i need to add something?