• Assignment – 8
Question-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: False.
(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: False.
(h) The ASCII character set consists of 128 distinct characters.
Answer: True.
(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-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: strcpy()
(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.
Answer: 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: strcmp().
(i) The function call strcat (s2,s1); appends …s1… to …s2….
Answer: s1, s2.
(j) The printf may be replaced by ……… function for printing trings.
Answer: Puts().
Question-3: 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: abnormal termination of program due to %s instead of %c
(d) for(i=0;string[i]!= “.”,i++)
printf(“%c”,string[i]);
output: error due to "." instead of '.'
(e) for(i=0;string[i]!= ‘\0’;i++)
printf(“%d\n”,string[i]);
output: ascii value of each character (84 104 101 32 115 107 121 32 105 115 32 108 105 109 105 116 46)
(f) for(i=0;i<=strlen[string]; ;)
{
string[i++]=i;
printf(“%s\n”,string[i]);
}
output: error due to wrong declaration of for statement
(g) printf(“%c\n”,string[10]+5);
output: %
(h) printf(“%c\n”,string[10]+5’);
output: error
Question - 4: Which of the following statements will correctly store the concatenation of strings s1 and s2 in string s3?
(a) s3=strcat (s1,s2);
Answer: error.
(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: false.
(f) strcpy(strcat(s1,s2),s3);
Answer: false.
Question -5: What will be the output of the following statements?
printf(“%d”,strcmp(“push”, “pull”));
output: 7
Question - 6: 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 7-: Find errors if any, in the following code segments;
(a) char str[10]
strncpy(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: What will be the output of the following segment ?
char s1[]= “Kolkata”;
char s2[]= “Pune”;
strcpy(s1, s2);
printf(“%s”,s1);
Output:
Pune
Question -9: What will be the output of the following segments ?
char s1[]= “NEW DELHI”
char s2[]= “BANGALORE”
strncpy(s1, s2, 3);
printf(“%s”, s1);
Output:
BAN DELHI
Question - 10: What will be the output of the following code?
char s1[]= “Jabalpur”
char s2[]= “Jaipur”
printf(strncmp(s1, s2, 2))
Output:
0
Question -11: 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 - 12: Compare the working of the following functions:
(a) stcpy and strncpy;
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) 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.
(C) 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 .
