C provides a number of in-built string handling functions which can be used directly on strings for getting length of strings, comparing, concatenating string and so on.
Following table lists some of these library functions and their purpose.
Let us see how strlen ( ), strcmp ( ), strcpy ( ), strcat ( ) can be implemented using arrays as well as pointers. Also how they are used will be seen with an example of each. The name of the string functions suggests their purpose in most cases. The declaration of all these functions is in the header file “string.h”. Hence it is a must to include this header file when we use them.
Note: functions implemented using array are appended with an ‘a’ at the beginning of the function name. functions implemented using pointers are appended with a ‘p’ at the beginning of the function name.
E.g. for strlen()
Name of function using arrays is astrlen ( )
Name of function using pointers is pstrlen ( )
As name suggests, this function is used to get the length of a string i.e. the number of character in a string, excluding the terminating character ‘�’.<Int strlen (const char * s);
It accepts address of a string as an argument and returns an integer i.e. length of a string. Can you imagine how this function would have been written? It is simple. Let us implement it using array of characters as well as pointer to character.
Implementing strlen ( )
Function strlen is already provided and its prototype is written in the header file string.h. We write function astrlen and pstrlen which imitate strlen. Function astrlen uses array to accommodate the string while function pstrlen uses a pointer equivalent to point to the string.
Array subscript version by using astrlen function
Pointer version by using pstrlen function
All that the function does is, it counts the number of character till string terminating character ‘�’ is encountered.
Using strlen() The following program shows use of function strlen to calculate length of character array arr.
This function compares two strings.int strcmp (const char * s, const char * t);
It accepts base address of two strings. It returns negative, zero or positive if ‘s’ is less than, equal to or
Function strcmp is already provided and its prototype is written in the header file string.h. We will write function astrcmp which imitate strcmp. Function astrcmp uses array to accommodate the string while function pstrcmp uses a pointer equivalent to point to the string.
Array subscript version/ * strcmp : will return
Strings ‘s’ and ‘t’ are compared character by character and the position where ‘s’ and ‘t’ do not match, the characters are subtracted which gives numeric difference, between ASCII values of non – matching characters, to get the return value. If end of string ‘s’ occurs before a mismatch, 0 is returned.
Using strcmp ()
The following program shows use of function strcmp to compare two strings.
Enter 2 Strings Susan
First string is equal to second string
This function copies the content of one string to another.char * strcpy (char *t, const char *s);
Base address of source string and target string are to be supplied to this function as arguments. It returns the target string.
Function strcpy is already provided and its prototype is written in the header file string.h. We write functions astrcpy which imitate strcpy. Function astrcpy uses array to accommodate the string while function pstrcpy uses ‘s’ pointer equivalent to point to the string
Array index version
Strcpy copies the characters in source string into the target string, one by one. Care is to be taken to see that target string is large enough to hold the entire source string.
The following program shows use if function strcpy to copy source string to target string
Source string = Sayonara
Target string = Sayonara
This function concatenates the source string at the end of the target string. Concatenating is appending or adding.
E.g. “Good” and “work’ on concatenation gives “Good work”.
It accepts base address of two strings to be concatenated as arguments and returns the target string.
Function strcat is already provided and its prototype is written in the header file string.h. We write function astrcat and pstrcat which imitate strcat. Function astrcat uses array to accommodate the string while function pstrcat uses a pointer equivalent to point to the string.
Array index version
The target string has to be large enough to hold the resulting string.
Using strcat() The following program shows use of function strcat to concatenate source string to target string.
Source string =Hello
Target string = world!
C Related Interview Questions
|DBMS Interview Questions||C++ Interview Questions|
|C & Data Structures Interview Questions||Java Interview Questions|
|Go (programming language) Interview Questions||F Sharp (programming language) Interview Questions|
|C preprocessor Interview Questions||R Programming language Interview Questions|
|D Programming Language Interview Questions||Lua (programming language) Interview Questions|
|Embedded C Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.