LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

array of int to string

Hello,
I have a problem maybe you can help me.
I have an array like this (for example) :  {101;102;103;104;106;107;108;112} and I would like to do a string like this : "101 to 104, 106 to 108,112".
Do you have any idea how to do it ?
 
Regards,
Francois
 
0 Kudos
Message 1 of 4
(3,164 Views)

Francois,

 

You can accomplish this with a for loop, sprintf, strcat, and some basic if-then logic.

 

Loop through each element in the array.  As you do, maintain a separate variable that is the next expected value (initialized to the first value in the array).  If the current array value doesn't match the expected value, then it's time to create a sub-string and add it to your main string.  The format of the string will depend on whether you've had more than one number in a row or not.  Once the sub-string is created, concatenate it to your main string.

 

Tony

0 Kudos
Message 2 of 4
(3,136 Views)
Will your array always be sorted?
0 Kudos
Message 3 of 4
(3,126 Views)

Good point about the sorting.  If it's not already sorted, you can sort it easily with qsort.

 

Tony

0 Kudos
Message 4 of 4
(3,122 Views)