LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Seeking for a more efficient method to have function like below.

I have three functions in CVI, which respectively are used to delete left space, right space of a string, and get a substring. I have the codes below. However, I'm seeking a more efficient method to do that, or I'm wondering whether NI already has available fp functions.

Thanks!
Jacky

/*delete left space of string*/
void Cut_Left_Space(char *s)
{
int i,j,k=0;
i=strlen(s)+1;
for(j=0;j if (s[j]!=' ') break;
for(k=0;j s[k]=s[j];
}



/*delete right space of string*/
void Cut_Right_Space(char *s)
{
int i,j;
i=strlen(s)-1;
for(j=i;j>-1;j--)
if (s[j]!=' ') break;
s[j+1]=0;
}



/*get substring*/
void Mid(char *s,char *t,int n,int m)
{
int i,j,p;
if(n<1) n=1;
i=strlen(s);
if(i if(m<0) m=i;
else m=n+m-1;
if(m>i) m=i;
p=m-n+1;
if(p<0) p=0;
for(i=n-1,j=0;i t[j]=s[i];
t[p]=0;
}
0 Kudos
Message 1 of 2
(3,219 Views)

The CVI Programmer's Toolbox has a function to remove leading and trailing spaces: RemoveSurroundingWhiteSpace.

To do your Mid function, you can use the ANSI C strncpy function or the Toolbox StringCopyMax function.  You do pointer addition to select the starting point

e.g.

char *myString = "0123456789";

char mySubStr[80];

int subStrStart = 2;

int subStrLen = 3;

if (subStrStart > strlen(myString))

   mySubStr[0] = '\0';

else

{

   strncpy(mySubStr, myString + subStrStart, subStrLen);

   mySubStr[subStrLen] = '\0';

}

You can also take a look at using while loops instead of for loops for some applications like your Cut_Right_Space function.

Instead of instead of using for(), if(), and break to find the last non-blank character, use a while loop.

i=strlen(s);

if (i>0)

{

   // do-nothing while loop finds the last non-blank character
   while (s[--i] == ' ' && i>0);

   // terminate the string after the while loop is done
   s[i+1]='\0';

}

 

0 Kudos
Message 2 of 2
(3,205 Views)