LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with string operation

HI,
 
Although it seems simple....I'm having difficulties to do this....
 
I'm reading with a bar code the UUT ID as follow:    "ABCDEFG    12"
 
The number of spaces may vary from 1 to 3.....also the second part may be of 2 or 3 characters.
 
I'm trying to get the last part into a buffer.  I'm having difficulties due to the fact that the number of spaces is not equal.
 
Can you help?
 
Thanks
 
0 Kudos
Message 1 of 5
(3,639 Views)
Use FindPattern to search for a single space starting from right: this will give you the index of the number after the spaces. Next strcpy (string + index, new_variable) and that's all.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 5
(3,630 Views)
In message <1164287412210-445070@exchange.ni.com>, Rafi2003 <x@no.email>
writes
>HI,
>&nbsp;
>Although it seems simple....I'm having difficulties to do this....
>&nbsp;
>I'm reading with a bar code the UUT ID as follow:&nbsp;&nbsp;&nbsp;
>"ABCDEFG&nbsp;&nbsp;&nbsp; 12"
>&nbsp;
>The number of spaces may vary from 1 to 3.....also the second part may
>be of 2 or 3 characters.
>&nbsp;
>I'm trying to get the last part into a buffer.&nbsp; I'm having difficulties due
>to the fact that the number of spaces is not equal.
>&nbsp;
>Can you help?
>&nbsp;
>Thanks
>&nbsp;

Hi,

I haven't tested this, but it should work.

char barCode[MAX_LEN], barCodeEnd[MAX_LEN];
int i;

strcpy(barCode, "ABCDEFG 12");

for( i = strlen( barCode ) ; 0 < i && isdigit( barCode[i] ) ; i-- );
++i;

if( i < strlen(barCode) && isdigit( barCode[i] ){
strcpy(barCodeEnd, &barCode[i]);
}
else{
strcpy(barCodeEnd, "No digits");
}

--
Regards,

John Cameron.
Type softly, read gently.
0 Kudos
Message 3 of 5
(3,625 Views)

Another possibility, supposing the first part is always and only alphabetic and the second always and only numeric, could be to use

Fmt (string, "%s[t#]%d", string1, number);

the [t#] modifier instructs the Scan function to terminate scanning the source string on the first numeric character. Scan and Fmt functions in the Formatting and I/O are powerful functions and are well documented in the online help.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 5
(3,602 Views)

Or you could just try:

 

 char Barcode[50] = {"ABCDEFG  \t123"};
 char  String[50];
 int  Number;
 
 sscanf(Barcode, "%s%d", String, &Number);

This doesn't care how many spaces or tabs are in between the alpha numeric field and the numeric field

 

Regards 

0 Kudos
Message 5 of 5
(3,594 Views)