String Manipulation


Functions

char* strstr (char *sz_str_1, char *sz_str_2)
char* strcpy (char *sz_destination, char *sz_source)
int strncpy (char *sz_destination, char *sz_source, size_t count)
int strcmp (char *sz_string_1, char *sz_string_2)
int strncmp (char *string_1, char *string_2, size_t count)
char* strchr (char *sz_source, char character)
char* strrchr (char *sz_source, int character)
bool strstarts (char *sz_source, char *sz_start_string)
bool strends (char *sz_source, char *sz_tail_string)
bool strmatch (char *sz_pattern, char *sz_source)
bool is_pattern (char *sz_source)
char* skipws (char *sz_source)
size_t strlen (char *sz_source)
char* strcat (char *sz_destination, char *sz_source)
char* trunc_spaces (char *sz_text)


Detailed Description

Some functions for string manipulations. Among these functions you can find ANSI C compatible ones( like strcpy(), strcmp() and others) as well as and functions specific to CyOS (is_pattern(), trunc_spaces(), strends()).


Function Documentation

bool is_pattern ( char * sz_source )
 

Checks whether string contains '*' or '?' characters.

Parameters:
sz_source   A source string
Returns:
TRUE if source string contains '*' or '?' characters, otherwise FALSE
       #include <cybiko.h>
       ...
       bool open_file( char* sz_name )
       {
         if( is_pattern( sz_name ) )
         {
           //  Searches file using pattern.
           ...
         }
         //  Opens file.
         ...
       }
       ...

char * skipws ( char * sz_source )
 

Skips white spaces.

Parameters:
sz_source   A source string
Returns:
The source string without white spaces.
        #include <cywin.h>
        ...
        char test_string[ 20 ];
        ...
        strcpy( test_string, "Any string" );
        //  Outputs: Any string
        TRACE( "%s", test_string );
        //  Outputs: Anystring
        TRACE( "%s", skipws( test_string ) );

char * strcat ( char * sz_destination,
char * sz_source )
 

Appends one string to another and terminates the resulting string with a null character.
The initial character of the source string overwrites the terminating null character of the destination string. No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap.

Parameters:
sz_destination   A null-terminated destination string.
sz_source   A null-terminated source string.
Returns:
The destination string
       #include <cybiko.h>
       ...
       const char sz_name = "world";
       char sz_string[20];
       ...
       strcpy( sz_string, "Hello," );
       strcat( sz_string, sz_name );
       strcat( sz_string, "!" );
       ...
       //  Traces "Hello, world!".
       TRACE( "Result: %s", sz_string);
       ...

char * strchr ( char * sz_source,
char character )
 

Finds a character in a string.

Parameters:
sz_source   A null-terminated source string
character   A character to be located
Returns:
A pointer to the first occurrence of a character in a string the source string, or NULL if a character is not found
       #include <cybiko.h>
       ...
       const char* sz_string = "I have 2 Cybiko computers!";
       char* ptr_amount;
       ...
       if( ( ptr_amount = strchr( sz_string, '2' ) ) )
       {
         *ptr_amount = '6';
       }
       //  Traces "I have 6 Cybiko computers!".
       TRACE( sz_string );
       ...

int strcmp ( char * sz_string_1,
char * sz_string_2 )
 

Compares characters from two strings.

Parameters:
sz_string_1   First null-terminated string to compare
sz_string_2   Second null-terminated string to compare
Returns:
0 if sz_string_1 is identical to sz_string_2. Positive value if sz_string_1 is greater (in lexical meaning) than sz_string_2. Negative value if sz_string_1 is greater (in lexical meaning) than sz_string_2.
       #include <cybiko.h>
       ...
       char* string_1 = "AAAAAAAAAB";
       char* string_2 = "AAAAAAAAAA";
       ...
       if( !strcmp( string_1, string_2 ) )
       {
         //  Strings are not identical, because
         //  string_1 is greater then string_2 in lexical meaning.
         ...
       }
       ...
See also:
strncmp.

char * strcpy ( char * sz_destination,
char * sz_source )
 

Copies a string, including the terminating null character to the destination buffer.
No overflow checking is performed when strings are copied or appended. The behavior of strcpy() is undefined if the source and destination strings overlap.

Parameters:
sz_destination   A destination buffer
sz_source   A null-terminated source string
Returns:
The destination string
       #include <cybiko.h>
       ...
       const char* sz_name = "world";
       char sz_string[20];
       ...
       strcpy( sz_string, "Hello," );
       strcat( sz_string, sz_name );
       strcat( sz_string, "!" );
       ...
       //  Traces "Hello, world!".
       TRACE( "Result: %s", sz_string);
       ...
See also:
strncpy.

bool strends ( char * sz_source,
char * sz_tail_string )
 

Checks whether the string ends with a specified substring.

Parameters:
sz_source   A pointer to the null-terminated source string.
sz_tail_string   A pointer to the null-terminated string to be located at the end of the source string.
Returns:
TRUE if the source string ends with the specified string.
       #include <cybiko.h>
       ...
       const char* name_list[6] = {
                                    "Dave",
                                     "Steve",
                                    "Jane",
                                    "Bill",
                                    "Robin",
                                    "John"
                                  };
       int index;
       ...
       //  Traces:
       //          "Names ending with e:"
       //          "Dave"
       //          "Steve"
       //          "Jane"
       TRACE("Names ending with e:");
       for( index = 0; index < 6 ; index ++)
       {
         if( strends( name_list[index], "e" ) )
         {
           TRACE("%s", name_list[index]);
         }
       }
       ...
See also:
strstarts.

size_t strlen ( char * sz_source )
 

Returns the length of a string, not including the terminating null character.

Parameters:
sz_source   A null-terminated source string
Returns:
The number of characters in the string
       #include <cybiko.h>
       ...
       char* sz_string;
       char* sz_source_string;
       ...
       sz_string = (char*)malloc( 10 );
       ...
       if( strlen( sz_source_string ) <= 10)
       {
         sz_string = (char*)realloc( sz_string,
                                     strlen( sz_source_string ) + 1 );
       }
       ...
       free(sz_string);
       ...

bool strmatch ( char * sz_pattern,
char * sz_source )
 

Checks wildcard match of the string to the pattern.
This function calculates a fast recursive pattern match using character '*' to match any characters with values of zero or more, and '?' to match exactly any single character. All other characters require the exact same ones as the testing string. Since this function is recursive, it does stack checking to prevent uncaught stack overflow. Checks wildcard match of a string to a pattern.

Parameters:
sz_pattern   A pointer to the pattern to check match
sz_source   A pointer to the source string
Returns:
TRUE if the pattern matches the source string, otherwise FALSE.
       #include <cybiko.h>
       ...
       const char* name_list[6] = {
                                    "Dave",
                                    "Steve",
                                    "Jane",
                                    "Bill",
                                    "Robin",
                                    "John"
                                  };
       int index;
       ...
       //  Traces:
       //          "Names with the "?a*" pattern:"
       //          "Dave"
       //          "Jane"
       TRACE("Names with the \"?a*\" pattern:");
       for( index = 0; index < 6 ; index ++)
       {
         if( strmatch( "?a*", name_list[index] ) )
         {
           TRACE("%s", name_list[index]);
         }
       }
       ...

int strncmp ( char * string_1,
char * string_2,
size_t count )
 

Compares characters from two strings.

Parameters:
sz_string_1   First null-terminated string to compare
sz_string_2   Second null-terminated string to compare
count   Number of characters to compare
Returns:
0 if sz_string_1 is identical to sz_string_2. Positive value if sz_string_1 is greater (in lexical meaning) than sz_string_2. Negative value if sz_string_1 is greater (in lexical meaning) than sz_string_2.
       char* string_1 = "AAAAAAAAAB";
       char* string_2 = "AAAAAAAAAA";
       ...
       if( strncmp( string_1, string_2, 6 ) )
       {
         //  Strings are identical, because we compare
         //  only the first 6 characters.
         ...
       }
       ...
See also:
strcmp.

int strncpy ( char * sz_destination,
char * sz_source,
size_t count )
 

Copies the initial count characters of the source string to the destination buffer.
If the count is less than or equal to the length of the source string, only count - 1 characters are copied from the source string, and a null character is appended automatically to the copied string. If the count is greater than the length of the source string, the destination string is padded with null characters up to the length count. The behavior of strncpy is undefined if the source and destination strings overlap.

Parameters:
sz_destination   A destination buffer
sz_source   A null-terminated source string
count   Number of characters to be copied
Returns:
The destination string
       #include <cybiko.h>
       ...
       const char* sz_string = "My nickname is CyCat. I love Cybiko!";
       char sz_nicname[NICKNAMESIZE + 1];
       ...
       strncpy( sz_string + 15, sz_nicname, NICKNAMESIZE + 1);
       //  Traces "His nickname is CyCat".
       TRACE("His nickname is %s", sz_nicname);
       ...
See also:
strcpy.

char * strrchr ( char * sz_source,
int character )
 

Scans a string for the last occurrence of a character.

Parameters:
sz_source   A pointer to a string.
character   A character to be located.
Returns:
A pointer to the last occurrence of character in string the source string, or NULL if character is not found.
       #include <cybiko.h>
       ...
       const char* sz_string = "I have 2 Cybiko computer! My friend Peter has 1 Cybiko computer.";
       char* ptr_amount;
       ...
       if( ( ptr_amount = strchr( sz_string, '1' ) ) )
       {
         *ptr_amount = '2';
       }
       //  Traces "I have 2 Cybiko computer! My friend Peter has 1 Cybiko computer".
       TRACE( sz_string );
       ...

bool strstarts ( char * sz_source,
char * sz_start_string )
 

Checks whether a string starts with a specified substring.

Parameters:
sz_source   A pointer to the null-terminated source string
sz_start_string   A pointer to the null-terminated string to be located at the beginning of the source string
Returns:
TRUE if the source string starts with the specified string
       #include <cybiko.h>
       ...
       const char* name_list[6] = {
                                    "Dave",
                                    "Steve",
                                    "Jane",
                                    "Bill",
                                    "Robin",
                                    "John"
                                  };
       int index;
       ...
       //  Traces:
       //          "Names beginning with J:"
       //          "Jane"
       //          "John"
       TRACE("Names beginning with J:");
       for( index = 0; index < 6 ; index ++)
       {
         if( strstarts( name_list[index], "J" ) )
         {
           TRACE("%s", name_list[index]);
         }
       }
       ...
See also:
strends.

char * strstr ( char * sz_str_1,
char * sz_str_2 )
 

Finds a substring.

Parameters:
sz_str_1   A null-terminated string to search
sz_str_2   A null-terminated string to search for
Returns:
A pointer to the first occurrence of sz_str_2 in sz_str_1
       #include <cybiko.h>
       ...
       char* sz_first_occurrence;
       char* sz_string = "The old string !";
       ...
       if( ( sz_first_occurrence = strstr( string, "old" ) ) )
       {
         memcpy( sz_first_occurrence, "new", 3 );
       }
       //  Traces "The new string !".
       TRACE( "Result: %s", sz_string );
       ...

char * trunc_spaces ( char * sz_text )
 

Removes spaces from the string.

Parameters:
sz_text   String to be modified
Returns:
A pointer to the string without spaces
        #include <cywin.h>
        ...
        char test_string[ 20 ];
        ...
        strcpy( test_string, "Any string" );
        //  Outputs: Any string
        TRACE( "%s", test_string );
        //  Outputs: Anystring
        TRACE( "%s", trunc_spaces( test_string ) );