The C compiler has a large set of valuable string handling library functions. Following table most commonly used functions along with their use.
Function |
Use |
strlen |
Determines the string's length |
strlwr |
Converts a string to lowercase |
strupr |
Converts a string to uppercase |
strcat |
Appends one string at the end of another string |
strncat |
Appends first n characters of a string at the end of another |
strcpy |
Copies a string into another |
strncpy |
Copies first n characters of one string into another |
strcmp |
Compares two strings |
strncmp |
Compares first n characters of two strings |
strcmpi |
Compares two strings without regard to case ("i" denotes that this function ignores case) |
stricmp |
Compares two strings without regard to case (identical to strcmpi) |
strnicmp |
Compares first n characters of two strings without regard to case |
strdup |
Duplicates a string |
strchr |
Finds first occurrence of a given character in a string |
strrchr |
Finds last occurrence of a given character in a string |
strstr |
Finds first occurrence of a given string in another string |
strset |
Sets all characters of string to a given character |
This function copies the contents of one string into another. The base addresses of the source and target strings should be lacking to this function.
#include<stdio.h>
#include<string.h>
int main( )
{
char one[ ] = "shreesanaviacademy" ;
char two[20] ;
strcpy ( two, one ) ;
printf ( "\n First string = %s", one ) ;
printf ( "\n Second string = %s", two ) ;
return 0;
}
First string = shreesanaviacademy
Second string = shreesanaviacademy
--------------------------------
Process exited after 2.442 seconds with return value 0
Press any key to continue . . .
This strlen( ) function counts a total number of characters present in a string.
#include<stdio.h>
#include<string.h>
int main( )
{
char arr[ ] = "shreesanavi" ;
int l1, l2 ;
l1 = strlen ( arr ) ;
l2 = strlen ( "shreesanaviacademy" ) ;
printf ( "\nstring = %s length = %d", arr, l1 ) ;
printf ( "\nstring = %s length = %d", "shreesanaviacademy", l2 ) ;
return 0;
}
string = shreesanavi length = 11
string = shreesanaviacademy length = 18
--------------------------------
Process exited after 1.155 seconds with return value 0
Press any key to continue . . .