II-162 Programming Concepts
17. What is the difference between the functions strcmp() and stricmp()?
Ans: The strcmp() function compares the two strings. It is case sensitive, i.e., if we have given
two strings ‘hello’ and ‘Hello’ as input to strcmp(), return value by strcmp() will not be zero,
instead it will be 32. This is due to difference between the ASCII values of two characters (Lower case
and upper case characters: example: Difference between ASCII values of a and A). Mismatch will be
found between h and H.
On the other hand, stricmp() is also used to compare the two strings, but it is not case sensitive;
therefore, the output given by stricmp() for above input will be zero, i.e., it will consider them as
same strings.
18. Give the format and explain the strlen().
Ans: The strlen() function counts the number of characters in a given string. The format of
function is:
strlen(char *string);
19. What is the use of strrev() and strlen() functions?
Ans: The strrev() function is used to reverse the input string, for example, if we give the string
‘apple’ as input to function strrev(), it will convert the string to its reverse as ‘elppa’.
The function strlen() is used to count the length of the string. The function strlen() will not
consider the null symbol, for example, if we give the string ‘Hello World! ’ as input to strlen(), the
value returned will be 12.
20. What is the use of strcpy() and strdup() functions?
Ans: The strcpy() function copies the contents of one string into another.
The syntax of strcpy() is:
strcpy(char *s2, char *s1)
where s1 is the source string, s2 is the destination string and s1 is copied to s2.
The strdup() function is used for duplicating a given string at the allocated memory which is
pointed by the pointer variable. The syntax of this function is:
char *strdup(char *)
21. What is the difference between strcpy() and strncpy() functions?
Ans: The function strcpy() is used to copy the source string into destination completely. The
syntax of strcpy() function is:
strcpy(char *,char *)
On the other hand, strncpy() is used to copy the part of the source string into destination, the
syntax of strncpy() function is:
strncpy(char *,char *,int)
The strncpy() function will behave same as that of strcpy(), when the parameter n is having
the value same that of length of source string.
M08_ITL-ESL4791_02_SE_C08.indd 162 12/22/2012 5:03:30 PM