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
Strings and Standard Functions II-163
22. What is the difference between ‘’ and ‘0’?
Ans:  ‘’ and ‘0 are not the same. The ASCII value of ‘0 is 48, whereas the ASCII value of
‘’ is 0.
23. What is the task of memcpy() memory function?
Ans:  The memcpy() function copies n number of characters from one string to another. The
memcopy() function copies the contents of source string to destination directly from physical
location.
24. Describe any three-string conversion functions.
Ans: The function atof() converts the given string to double, the syntax of function at
of() is:
double atof(const char *string);
The function atoi() is used to converts the given string to int, the syntax of function atoi() is:
int atoi(const char *string);
The function strtod() separates char and float data from the given string.
25. What is the use of atof() function?
Ans: The function atof() is used to convert the given string into double. The syntax of
function atof() is:
double atof(const char *string);
26. What is the use of strlwr() function? Give its syntax.
Ans: This function can be used to convert any string to a lower case. When you are passing any
upper case string to this function, it converts it into lower case. The standard format of this function is
as follows:
Char *strlwr(char *string);
27. What is the use of strupr() function? Give its syntax.
Ans: The strupr() function is the same as strlwr(), but the difference is that strupr()
converts lower case strings into upper case. The format of this function is:
Char *strupr(char *string);
28. What is the use of strcat() function? Give its syntax.
Ans: The strcat() function appends the target string to the source string. Concatenation of the
two strings can be done using this function. The format of this function is:
Char *strcat(char *des,char *src);
29. What is the use of strlen() function? Give its syntax.
Ans: The strlen()function counts the number of characters in a given string. The length of the
string is calculated by this function. The format of function is:
size_t strlen(char *string);
M08_ITL-ESL4791_02_SE_C08.indd 163 12/22/2012 5:03:30 PM
II-164 Programming Concepts
30. What is the use of strcpy() function? Give its syntax.
Ans: The strcpy()function copies the contents of one string to another. The format of this func-
tion is:
char *strcpy(char *s2, const char *sl);
where s1 is source string, s2 is destination string and s1 is copied to s2.
31. What is the use of strstr() function? Give its syntax.
Ans: The strstr() function finds second string in the first string. It returns the pointer location
from where the second string starts in the first string. In case the first occurrence in the string is not
observed, the function returns a NULL character. The format of this function is:
char *strstr(char *string1,char *string2);
Multiple-choice Questions
1. A string in C is declared as .
(a) Character array (b) String array
(c) long char array
2. The string always ends with .
(a) NULL char (b) char (c) ! char
3. Which of the following function is appropriate to duplicate the string?
(a) strdup() (b) strupr() (c) strspn()
4. Which function you will use for coping string from source to destination?
(a) strcpy() (b) strrev() (c) strlwr()
5. All the string functions are defined in .
(a) string.h (b) math.h (c) iostream.h
6. Which of the following one is not a string constant?
(a) “String” (b) “C” (c) ‘C’
7. Which of the following unformatted function is used to read string through keyboard?
(a) scanf() (b) gets() (c) None of the above
8. The NULL char is useful for compiler to .
(a) Detect the end of the string (b) Detect the beginning of the string
(c) Search in the memory
9. c[] = ‘CPLUSPLUS’ in order to print the ‘CPP’ the statement will be .
(a) printf("%c %c %c",c[0],c[1],c[5]);
(b) printf("%s %s %s",c[0],c[1],c[5]);
(c) printf("%s %s %s",c[1],c[2],c[6]);
10. Which of the following function is more appropriate for reading string with spaces?
(a) printf() (b) gets() (c) scanf()
M08_ITL-ESL4791_02_SE_C08.indd 164 12/22/2012 5:03:30 PM
Strings and Standard Functions II-165
11. The strlen() function returns the length of the string including .
(a) All characters, spaces and symbols, numbers excluding NULL char
(b) All characters, spaces and symbols, numbers and NULL char
(c) Characters, numbers and excluding spaces
12. char x[] = "Program"; the sizeof(x) will return .
(a) 7 (b) 8 (c) 9
13. The string always ends with .
(a) ‘’ character (b) ‘’ character
(c) ‘0’ character (d) None of the above
14. The character also occupies memory space.
(a) NULL char (b) ‘^’ (c) ?
15. When a string is reversed, the NULL char will be at the .
(a) Beginning of string (b) End of string (c) Inside the string
16. “B” is a and ‘B’ is a
.
(a) string constant; char constant (b) char constant; string constant
(c) string constant; string constant
17. The array char name[8] can hold maximum characters excluding NULL.
(a) 8 (b) 7 (c) 6
18. For the declaration such as char x [ ] = "abcd", the size of array determined
by .
(a) Compiler (b) OS (c) Linker
19. The library function strncpy() requires arguments.
(a) 1 (b) 2 (c) 3
20. The strlen() function requires arguments.
(a) 1 (b) 2 (c) 3
21. The library function strcpy() requires arguments.
(a) 1 (b) 2 (c) 3
22. The strlen() function requires arguments.
(a) 1 (b) 2 (c) 3
23. The string function compares characters of two strings up to the given length.
(a) strcmp() (b) strncmp() (c) strrcmp()
24. The string function strupr() converts .
(a) Converts lowercase characters of a string to uppercase
(b) Converts lowercase characters of a string to title case
(c) Converts lowercase characters of a string to sentence case
25. The string function strncpy() copies .
(a) Copies all characters of a string to another string
(b) Copies characters of a string to another string up to the specified length
(c) None of the above
M08_ITL-ESL4791_02_SE_C08.indd 165 12/22/2012 5:03:30 PM
II-166 Programming Concepts
26. function finds second string in the first string.
(a) strstr() (b) strnstr() (c) strsearch()
27. function appends the target string to the source string. Concatenation of two strings
would be done using this function.
(a) strcat() (b) strncat() (c) strrcat()
28. A + = ‘a’.
(a) 33 (b) 32 (c) 12
29. The ASCII equivalent of 32 is .
(a) ? (b) – (c) Space
30. The statement printf("%d",' ') will print .
(a) 34 (b) 32 (c) 31
31. The atoi(string) converts a string of digits in to .
(a) Integers (b) Floating number (c) Characters
32. What will be the output of the program?
void main()
{
char nm[]={'A','N','S','I',0,'C',''}; int x=0;
clrscr();
while(nm[x]!='')
printf("%c",nm[x++]);
}
(a) ANSI (b) ANSI0C
(c) ANSIC (d) None of the above
33. What will be the size of character array?
void main()
{
char x[]={'s','a',''};
printf(" %d", sizeof(x));
}
(a) 3 (b) 2
(c) 0 (d) None of the above
34. What will be the output of the following program?
#include <string.h>
void main()
{
M08_ITL-ESL4791_02_SE_C08.indd 166 12/22/2012 5:03:30 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset