Strings and Standard Functions II-167
char x[]="a1b2c3d4e5f6g7h8i9j0";
int t=0;
clrscr();
for(t=l;x[t]!=0 && t<=strlen(x);t+=2)
printf("%c",x[t]);
}
(a) 1234567890 (b) abcdefghij
(c) a1b2c3d4e5f6g7h8i9j0 (d) None of the above
35. What will be the output of the following program?
void main()
{
char txt[]="12345abcdef";
clrscr();
printf("%s",txt);
}
(a) 12345 (b) abcdef
(c) 12345abcdef (d) None of the above
36. String contains .
(a) Characters (b) Numbers
(c) Symbols (d) All of these
37. Every string has a termination character called as null denoted by .
(a) ‘/0’ (b) ‘’
(c) ‘0/’ (d) ‘0’
38. String is a .
(a) One-D character array (b) Two-D character array
(c) Three-D character array (d) All of these
39. The size of string in bytes is .
(a) Equal to the length of string (b) Equal to one more than length of string
(c) Equal to one less than length of string (d) None of the above
40. One-D array of string is .
(a) One-D character array (b) Two-D character array
(c) Three-D character array (d) None of the above
41. The correct syntax of string is .
(a) char c[] = "ABC"; (b) char c[] = {‘A','B','C'};
(c) char c[5]; (d) c[0] = 'A'; c[1] = 'B'; c[2] = 'C';
(e) All of the above
42. Choose incorrect one .
(a) char c[] = {"A","B","C"}; (b) char c[] = "ABC";
(c) char c[] = {'A','B','C'}; (d) char c[5]; c = "ABC";
M08_ITL-ESL4791_02_SE_C08.indd 167 12/22/2012 5:03:30 PM
II-168 Programming Concepts
43. Which of the following are string functions?
(a) strcat() (b) strstr()
(c) strcpy() (d) strupr()
(e) All of these
44. Which of the following is not a string function?
(a) strchr() (b) strset()
(c) strrev() (d) strvcat()
(e) strcmpi()
45. The characters in a string are
(a) of 1 byte each (b) stored in contiguous memory locations
(c) Both a and b (d) None
Answers
1. (a) 2. (a) 3. (a) 4. (a) 5. (a) 6. (c) 7. (b) 8. (a) 9. (a) 10. (b)
11. (a) 12. (b) 13. (a) 14. (a) 15. (b) 16. (a) 17. (b) 18. (a) 19. (c) 20. (a)
21. (b) 22. (a) 23. (b) 24. (a) 25. (b) 26. (a) 27. (a) 28. (b) 29. (c) 30. (b)
31. (a) 32. (a) 33. (a) 34. (a) 35. (a) 36. (d) 37. (b) 38. (a) 39. (b) 40. (b)
41. (d) 42. (d) 43. (e) 44. (d) 45. (c)
True or False
1. A string is defined as an array of characters.
2. Every string ends with a NULL character.
3. The declaration char name[ ] ="India"; is invalid.
4. The strlen() function returns the length of the given string.
5. The strdup() is used to copy the string.
6. The sricmp() and strncmp() are exactly same.
7. While reversing the string, address in memory also get changed.
8. If pre-existing string is replaced with new, memory addresses remain same.
9. If the string is reversed, the NULL character appears first.
10. The length of the string includes NULL character.
11. "12345" is a valid string.
12. 'A' is a valid string.
13. If two strings are merged, the string contains two NULL characters.
14. The string is declared as character array.
15. The string is not stored in successive memory locations.
16. The char constant 'C' ends with NULL character.
M08_ITL-ESL4791_02_SE_C08.indd 168 12/22/2012 5:03:30 PM
Strings and Standard Functions II-169
17. The format string %c is used to display string.
18. The NULL char and 0 are treated the same by a compiler.
19. 'A' > 'a'.
20. The function strstr() finds second string in the first string.
Answers
1. True 2. True 3. False 4. True 5. True 6. False 7. False 8. True 9. False 10. False
11. True 12. False 13. False 14. True 15. False 16. False 17. False 18. True 19. False 20. True
Additional Questions
What are the outputs of the following programs?
Programs to display string and string operations.
1. Display a string “MAHARASHTRA”
#include <string.h>
void main()
{
char *a1[12]={"MAHARASHTRA"};
clrscr();
printf("%s ",*a1);
getche();
}
OUTPUT:
MAHARASHTRA
2. Display a string "NAGPUR"
#include <string.h>
void main()
{
char a1[6]={"NAGPUR"};
int j;
clrscr();
for(j=0;j<6;j++)
printf("%c ",a1[j]);
++j;
getche();
}
OUTPUT:
N A G P U R
M08_ITL-ESL4791_02_SE_C08.indd 169 12/22/2012 5:03:30 PM
II-170 Programming Concepts
3. Display strings “MAHARASHTRA” & “MADHYAPRADESH” with and without pointer
#include <string.h>
void main()
{
char a1[12]="MAHARASHTRA";
char *a2[13]={"MADHYAPRADESH"};
clrscr();
printf("%s ",a1);
printf("%s ",*a2);
getche();
}
OUTPUT:
MAHARASHTRA MADHYAPRADESH
4. Display strings “KOLHAPUR” & “NAGPUR” with and without pointer
#include <string.h>
void main()
{
char a1[7]={"NAGPUR"};
char *n="SOLAPUR";
clrscr();
n="KOLHAPUR",
printf("%s %s ",*&(n),a1);
getche();
}
OUTPUT:
KOLHAPUR NAGPUR
5. Display text with different formats
void main()
{
char text[15]="MOUNTAIN";
clrscr();
printf("%.5s ",text);
printf("%.8s ",text);
getche();
}
OUTPUT:
MOUNT
MOUNTAIN
M08_ITL-ESL4791_02_SE_C08.indd 170 12/22/2012 5:03:30 PM
Strings and Standard Functions II-171
6. Program on encryption of a string
void main()
{
char text[15]="MOUNTAIN";
int i;
clrscr();
for(i=0;i<=7;i++)
printf("%.8c ",text[i]+2);
getche();
}
OUTPUT:
O Q W P V C K P
7. Count the number of spaces in the sentence using while and if loop.
#include <stdio.h>
#include <conio.h>
void main()
{
char sentence[20]="Hello! How R U?";
int count=0,i=0;
clrscr();
printf(" %s",sentence);
while(sentence[i++]!='')
if(sentence[i]==32 || sentence[i]=='')
count++;
printf(" %d",count);
getche();
}
OUTPUT:
Hello! How R U?
4
8. Sorting the list of places without using string functions
#include <stdio.h>
#include <conio.h>
void main()
{
char place[5][20]={"Delhi","Chennai","Bangalore","Mumbai",
"Nanded"};
M08_ITL-ESL4791_02_SE_C08.indd 171 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