Review question’s answer
Chapter no-08(Character
array and string)
===============================
RQ-8.1: State whether the following statements
are true or false.
(a) When initializing a
string variable during its declaration, we must include the null character as
part of the string constant, like ‘GOOD\0’.
Answer: False.
(b) The gets function
automatically appends the null character at the end of the string read
from the keyboard.
Answer: True.
(c) When reading a string
with scanf, it automatically inserts the terminating null character.
Answer: True.
(d) String variables cannot
be used with the assignment operator.
Answer: True.
(e) We cannot perform
arithmetic operations on character variables.
Answer: True.
(f) We can assign a
character constant or a character variable to an int type variable.
Answer: False.
(g) The function scanf()
cannot be used in any way to read a line of text with the white spaces.
Answer: True.
(h) The ASCII character set
consists of 128 distinct characters.
Answer: False.
(i) In the ASCII
collating sequence, the uppercase letters precede lowercase letters.
Answer: True.
(j) In C, it is illegal to
mix character data with numeric data in arithmetic operations.
Answer: False.
(k) The function getchar
skips white-space during input.
Answer: False.
(l) In C, strings cannot be
initialized at run time.
Answer: False.
(m) The input function gets
has one string parameter.
Answer: True.
(n) The function call
strcpy (s2, s1); copies string s2 into string s1.
Answer: False.
(o) The function call
strcmp ( ‘abc’ , ‘ABC’ ); returns a positive number.
Answer: True.
Question-8.2 Fill in the
blanks in the following statements.
(a) We can use the
conversion specification ….. in scanf to read a line of text.
Answer: code.
(b) We can initialize a
string using the string manipulation function ……...
Answer:
(c) The function strncat
has (three) parameters.
Answer: three
(d) To use the function
atoi in a program, we must include the header file ……. .
Answer: <std.lib.h>
(e) The function ……. does
not require any conversion specification to read a string from the keyboard.
Answer: gets.
(f) The function ……… is
used to determine the length of a string.
Ans: strlen.
(g) The ……….string manipulation
function determines if a character is contained in a string.
Answer: strstr.
(h) The function ………..is
used to sort the strings in alphabetical order.
Answer: ASCII.
(i) The function call
strcat (s2,s1); appends …… to …….
Answer: One, another.
(j) The printf may be
replaced by ……… function for printing trings.
Answer: Puts.
Question-8.3: Describe the limitations of using
getchar and scanf functions for reading strings.
Answer:
By using getchar we can
read only one character from the keyboard.
We can read only string
without white spaces by using scanf function.
Question-8.4: Character strings in C are automatically
terminated by the null character.
Explain how this feature
helps in string manipulations.
Answer: We know that a
string is not a data types in c, but it is consider a data structure stored in
array. The string is a variable-length structure and is stored in a
fixed-array. therefore, the last element of an array need not be represent at
the end.
It is automatically
terminate by null character.
Question-8.5: Strings can
be assigned values as follows:
(a) During type
declaration
char string[]={“.........”};
Answer: Read a character
string.
(b) Using strcpy function
strcpy(string, “......”);
Answer: Copy one string to
another.
(c) Reading using scanf function
scanf(“%s”,string);
Answer: It takes a string.
(d) Reading using gets function
gets(string);
Answer: Read a line of
string.
Compare them critically and
describe situations where one is superior to others.
Question-8.6: Assuming the
variable string contain the value “ The sky is the limit ”,
determine
what output of the following segments will be.
(a) printf (“%s”,string);
output:
The sky is the limit
(b)
printf(“%25.10s”,string);
output:
The sky is
(c) printf(“%s”,string[0]);
output:
.
(d) for(i=0;string[i]!=
“.”,i++)
printf(“%c”,string[i]);
output:
(e) for(i=0;string[i]!=
‘\0’;i++)
printf(“%d\n”,string[i]);
output:
(f)
for(i=0;i<=strlen[string]; ;)
{
string[i++]=i;
printf(“%s\n”,string[i]);
}
output:
(g)
printf(“%c\n”,string[10]+5);
output:
(h)
printf(“%c\n”,string[10]+5’);
output:
Question-8.7: Which of the
following statements will correctly store the concatenation
of strings
s1 and s2 in string s3?
(a) s3=strcat (s1,s2);
Answer: correct.
(b) strcat(s1,s2,s3);
Answer: error.
(c) strcat(s3,s2,s1);
Answer: error.
(d)
strcpy(s3,strcat(s1,s2));
Answer: correct.
(e)
strcmp(s3,strcat(s1,s2));
Answer: correct.
(f)
strcpy(strcat(s1,s2),s3);
Answer: error.
Question-8.8: What will be
the output of the following statements?
printf(“%d”,strcmp(“push”, “pull”));
output:
7
Question-8.9: Assume that
s1, s2 and s3 are declared as follows:
char s1[10]= “he”, s2[20]= “she”, s3[30], s4[30];
What will be the
output of following statements executed in sequence?
printf(“%s”, strcpy(s3, s1));
printf(“%s”, strcat(strcat(strcpy(s4,s1),“or”),s2));
printf(“%d %d”,strlen(s2)+strlen(s3),strlen(s4));
output:
he
heorshe
5 7
Question-8.10: Find errors
if any, in the following code segments;
(a) char str[10]
strcpy(str, “GOD”, 3);
printf(“%s”,str);
Answer: error.
Correct answer: char str[10];
strcpy(str,
“GOD”, 3);
printf(“%s”,str);
(b) char str[10];
strcpy(str, “Balagurusamy”);
Answer: no error.
(c)
if strstr(“balagurusamy”, “guru”)==0);
printf(“Substring is found”);
Answer: no error.sss
(d) char s1[5], s2[10],
gets(s1, s2);
Answer: error.
Correct answer: char s1[5], s2[10];
gets(s1);
gets(s2);
Question-8.11: What will be
the output of the following segment ?
char s1[]= “Kolkata”;
char s2[]= “Pune”;
strcpy(s1, s2);
printf(“%s”,s);
output:
Pune
Question-8.12: What will be
the output of the following segment s?
char s1[]= “NEW DELHI”
char s2[]= “BANGALORE”
strcpy(s1, s2, 3);
printf(“%s”, s1);
output:
BAN DELHI
Question-8.13: What will be
the output of the following code?
char s1[]= “Jabalpur”
char s2[]= “Jaipur”
printf(strncmp(s1, s2, 2))
output:
0
Question-8.14: What will be
the output of the following code?
char s1[]= “ANIL KUMAR
GUPTA”
char s2[]= “KUMAR”
printf(strstr(s1,s2));
output:
KUMAR GUPTA
Question-8.15: Compare the
working of the following functions:
(a) stcpy and strncpy;
(b) Answer: The function
strcpy copies one string to another string but the
function strncpy copies only the
left-most n characters of the source string
to the target string
variable.
(b) strcmp and
strncmp; and
Answer:
The strcmp function compares two strings identified
by the arguments but
strncmp
function compares the left-most n characters of two string .
(c) strcat and
strncat
Answer: The
function strcat joins two
strings together but the function strncat
concanate left-most n characters of target string to source string.
- You can download this file as pdf by clicking below download icon-
- Download
Go ahead by honesty--->Rm
No comments:
Post a Comment