The example below demonstrates the use of alternative C library functions for basic string operations.
#include <stdio.h>
#include "ST_API.h"
void PrintCmpResult(int Cmp)
{
if(Cmp < 0)
printf("String1 is less than String2.\n");
if(Cmp > 0)
printf("String1 is greater than String2.\n");
if(Cmp == 0)
printf("String1 is equal to String2.\n");
}
int main(void)
{
int Len1, Len2, Cmp;
char String1[64];
char String2[64];
/* Initialization */
arInit();
stInit();
/* Copy string */
stStrCpy(String1, "C:\SpaceShadow");
printf("String1: '%s'\n", String1);
/* Append string */
stStrCat(String1, "\OS");
printf("String1: '%s'\n", String1);
/* Copy string with truncation */
stStrNCpy(String2, String1, 14);
printf("String2: '%s'\n", String2);
/* Append string with truncation */
stStrNCat(String2, "\STD\ST_API.h", 18);
printf("String2: '%s'\n", String2);
/* Convert letters to upper case */
stStrUpr(String2);
printf("String2: '%s'\n", String2);
/* Get string length */
Len1 = stStrLen(String1);
Len2 = stStrLen(String2);
printf("String1 Length: %i\n", Len1);
printf("String2 Length: %i\n", Len2);
/* Compare strings */
Cmp = stStrCmp(String1, String2);
PrintCmpResult(Cmp);
/* Compare specified number of characters of the string */
Cmp = stStrNCmp(String1, String2, 14);
PrintCmpResult(Cmp);
/* Compare specified strings without case sensitivity */
Cmp = stStrICmp(String1, String2);
PrintCmpResult(Cmp);
/* Compare specified number of characters of the strings */
/* without case sensitivity */
Cmp = stStrNICmp(String1, String2, 14);
PrintCmpResult(Cmp);
/* Deinitialization */
arDeinit();
return 0;
}
The console output should look as follows:
String1: 'C:\SpaceShadow' String1: 'C:\SpaceShadow\OS' String2: 'C:\SpaceShadow' String2: 'C:\SpaceShadow\STD\ST_API.h' String2: 'C:\SPACESHADOW\STD\ST_API.H' String1 Length: 17 String2 Length: 27 String1 is greater than String2. String1 is greater than String2. String1 is less than String2. String1 is equal to String2.