C STRING FUNCTIONS:
C STRING FUNCTIONS:
- String.h header file supports all the string functions in C language. All the string functions are given below.
- Click on each string function name below for detail description and example programs.
String functions
|
Description
|
strcat ( ) | Concatenates str2 at the end of str1 |
strncat ( ) | Appends a portion of string to another |
strcpy ( ) | Copies str2 into str1 |
strncpy ( ) | Copies given number of characters of one string to another |
strlen ( ) | Gives the length of str1 |
strcmp ( ) | Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 > str2 |
strcmpi ( ) | Same as strcmp() function. But, this function negotiates case. “A” and “a” are treated as same. |
strchr ( ) | Returns pointer to first occurrence of char in str1 |
strrchr ( ) | last occurrence of given character in a string is found |
strstr ( ) | Returns pointer to first occurrence of str2 in str1 |
strrstr ( ) | Returns pointer to last occurrence of str2 in str1 |
strdup ( ) | Duplicates the string |
strlwr ( ) | Converts string to lowercase |
strupr ( ) | Converts string to uppercase |
strrev ( ) | Reverses the given string |
strset ( ) | Sets all character in a string to given character |
strnset ( ) | It sets the portion of characters in a string to given character |
strtok ( ) | Tokenizing given string using delimiter |
C – strcpy() function
- strcpy( ) function copies contents of one string into another string. Syntax for strcpy function is given below.
char * strcpy ( char * destination, const char * source );
- Example:
strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.
- If destination string length is less than source string, entire source string value won’t be copied into destination string.
- For example, consider destination string length is 20 and source string length is 30. Then, only 20 characters from source string will be copied into destination string and remaining 10 characters won’t be copied and will be truncated.
EXAMPLE PROGRAM FOR STRCPY( ) FUNCTION IN C:
In this program, source string “fresh2refresh” is copied into target string using strcpy( ) function.
OUTPUT:
source string = fresh2refresh
target string = target string after strcpy( ) = fresh2refresh |
C – strcat() function
strcat( ) function in C language concatenates two given strings. It concatenates source string at the end of destination string. Syntax for strcat( ) function is given below.
char * strcat ( char * destination, const char * source );- Example:
strcat ( str2, str1 ); – str1 is concatenated at the end of str2.
strcat ( str1, str2 ); – str2 is concatenated at the end of str1.
- As you know, each string in C is ended up with null character (‘\0’).
- In strcat( ) operation, null character of destination string is overwritten by source string’s first character and null character is added at the end of new destination string which is created after strcat( ) operation.
EXAMPLE PROGRAM FOR STRCAT( ) FUNCTION IN C:
In this program, two strings “fresh2refresh” and “C tutorial” are concatenated using strcat( ) function and result is displayed as “C tutorial fresh2refresh”.
OUTPUT:
Source string = fresh2refresh
Target string = C tutorial Target string after strcat( ) = C tutorial fresh2refresh |
C – strlen() function
- strlen( ) function in C gives the length of the given string. Syntax for strlen( ) function is given below.
- strlen( ) function counts the number of characters in a given string and returns the integer value.
- It stops counting the character when null character is found. Because, null character indicates the end of the string in C.
EXAMPLE PROGRAM FOR STRLEN() FUNCTION IN C:
In below example program, length of the string “fresh2refres.com” is determined by strlen( ) function as below. Length of this string 17 is displayed as output.
OUTPUT:
string length = 17
|
C – strcmp() function
strcmp( ) function in C compares two given strings and returns zero if they are same.
- If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns > 0 value. Syntax for strcmp( ) function is given below.int strcmp ( const char * str1, const char * str2 );
- strcmp( ) function is case sensitive. i.e, “A” and “a” are treated as different characters.
EXAMPLE PROGRAM FOR STRCMP( ) FUNCTION IN C:
In this program, strings “fresh” and “refresh” are compared. 0 is returned when strings are equal. Negative value is returned when str1 < str2 and positive value is returned when str1 > str2.
OUTPUT:
0 -1 1
|
C – strlwr() function
strlwr( ) function converts a given string into lowercase. Syntax for strlwr( ) function is given below.
char *strlwr(char *string);
char *strlwr(char *string);
- strlwr( ) function is non standard function which may not available in standard library in C.
EXAMPLE PROGRAM FOR STRLWR() FUNCTION IN C:
In this program, string “MODIFY This String To LOwer” is converted into lower case using strlwr( ) function and result is displayed as “modify this string to lower”.
OUTPUT:
modify this string to lower
|
C – strupr() function
PREV NEXT
- strupr( ) function converts a given string into uppercase. Syntax for strupr( ) function is given below.
char *strupr(char *string);
- strupr( ) function is non standard function which may not available in standard library in C.
EXAMPLE PROGRAM FOR STRUPR() FUNCTION IN C:
In this program, string “Modify This String To Upper” is converted into uppercase using strupr( ) function and result is displayed as “MODIFY THIS STRING TO UPPER”.
OUTPUT:
MODIFY THIS STRING TO UPPER
|
C – strrev() function
- strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is given below.char *strrev(char *string);
- strrev( ) function is non standard function which may not available in standard library in C.
EXAMPLE PROGRAM FOR STRREV() FUNCTION IN C:
In below program, string “Hello” is reversed using strrev( ) function and output is displayed as “olleH”.
OUTPUT:
String before strrev( ) : Hello
String after strrev( ) : olleH |
Comments
Post a Comment