Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

String manipulation Functions of the String Library string.h

September 19, 2013 Posted by: GPA Views: 779 0 comments

<string.h> library includes functions that allow to manipulate arrays of characters (copying strings and concatenating strings, comparing strings, searching strings for characters and other manipulation). About string you can read here Using Strings and Characters in C Programming and  Using Character Arrays to Store Strings in C programming.

Copying and concatenating strings functions.

Function  Description

char *strcpy( char * firstString , const char * secondString )

Copies string secondString into array firstString. The value of firstString is returned.
char *strncpy( char * firstString , const char * secondString , size_t n ) Copies at most n characters of string secondString into array firstString. The value of firstString is returned.
char *strcat( char * firstString , const char * secondString )
Appends string secondString to array firstString.The value of  firstString is returned.
char *strncat( char * firstString , const char * secondString , size_t n ) Appends at most n characters of string secondString to array firstString.  The value of firstString is returned.

Comparison Functions.

These functions allow to compare strings. 

Function  Description

int strcmp( const char * firstString , const char * secondString );

Compares the string firstString with the string  secondString. The function returns 0, less than 0 or greater than 0 if firstString is equal to, less than or greater than secondString.
int strncmp( const char * firstString , const char * secondString , size_t n ); Compares up to n characters of the string firstString with the string secondString. The function returns 0, less than 0 or greater than 0 if firstString is equal to, less than or greater than secondString.

Search Functions.

These functions allow to search strings for characters and other strings.

Function  Description

char *strchr( const char *str, int c );

Searches  the first occurrence of character c in string s. If c is found, a pointer to c in str is returned. Otherwise, a NULL pointer is returned.
size_t strcspn( const char * firstString , const char * secondString);

Determines and returns the length of the initial segment of string firstString consisting of characters not contained in string secondString .

size_t strspn( const char * firstString , const char * secondString ); Determines and returns the length of the part of string  firstString consisting only of characters contained in string secondString.
char *strpbrk( const char * firstString , const char * secondString); Searches the first occurrence in string firstString of any character in string secondString . If a character from string secondString is found, a pointer to the character in string firstString  is returned. Otherwise, a NULL pointer is returned.
char *strrchr( const char *str, int c ); Searches  the last occurrence of c in string str. If c is found, a pointer to c in string str is returned. Otherwise, a NULL pointer is returned.
char *strstr( const char * firstString , const char * secondString); Searches  the first occurrence in string firstString of string secondString . If the string is found, a pointer to the string in firstString is returned. Otherwise, a NULL pointer is returned.
char *strtok( char * firstString , const char * secondString); Breaks firststring  into a series of tokens separated by secondString.

Memory Functions.

These functions below allow to manipulate, to compare and to search blocks of memory. The functions treat blocks of memory as character arrays and can manipulate any block of data. 

 

Function  Description

void *memcpy( void * firstString , const void * secondString, size_t n );

Copies n characters from the string pointed to by firstString  into the string pointed to by secondString. A pointer to the resulting object is returned.
void *memmove( void * firstArgument, const void * secondArgument , size_t n );
Copies n characters from the string pointed to by  firstArgument into the string pointed to by secondArgument. A pointer to the resulting object is returned.
int memcmp( const void *firstString, const void *secondString, size_t n);
Compares the first n characters of the string pointed to by firstString and secondString. The function returns 0, less than 0 or greater than 0 if firstString is equal to, less than or greater than secondString.  
void *memchr( const void *str, int c, size_t n ); Searches the first occurrence of c in the first n characters of the object pointed to by str. If c is found, a pointer to c in the object is returned. Otherwise, NULL is returned.
void *memset( void *str, int c, size_t n );
Copies c into the first n characters of the string pointed to by str.

Other Functions.

The two remaining functions of this library are strerror and strlen.

Function  Description

char *strerror(int errornumber);

Searches  for the error number errornumber into a string. A pointer to the string is returned.
size_t strlen(const char *str); Determines the length of string str.

 

 

 total number of characters

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe