CC-112 Programming Fundamentals Spring 2022
Quiz 12 – Chapter 08 - Characters and Strings [40 Marks, 30 Minutes]
MAbdullah
Name: _____________________________ 133715
Roll No: _____________________________
Question 1: [2x8 marks] [estimated time 8 minutes] char color [] = {'b', 'l', 'u', 'e'};
Supply the missing words to complete the statements.
b) (T / F) Printing a string that does not contain a
1. A string is accessed via a to the string’s first terminating null character is a logic error—program
character. ___________________.
pointer execution terminates immediately.
2. What is the right way to initialize an array? c) (T / F) Function strcpy copies its first argument
int num[6] = { 2, 4, 12, 5, 45, 5 }; (a string) into its second argument,
int n {} = { 2, 4, 12, 5, 45, 5 }; d) (T / F) strcmp (s1, s2) return 0 if s1 and s2
int n{6} = { 2, 4, 12 }; strings are equal
int n(6) = { 2, 4, 12, 5, 45, 5 }; e) (T / F) PUTS can print a multi-word string.
3. int iscntrl (int c); Returns a true value if c f) (T / F) String is an array of Characters with a
___________
is a control character. null character as the last element of an array.
4. int isgraph (int c); Returns a true value if c is a
Question 3: [2x5 marks] [estimated time 6 minutes]
space
printing character other than a ________________ Dry Run the pseudocode and write the expected
5. Which function is described as “Returns a true value output against the input. (Write “Blank” for no output
or write “compile Error” if there is any error).
if the argument character is a digit or a letter;
otherwise, returns 0 (false)”? 1. Write the output of each printf in a separate line
given at the end:
a) isalnum.
b) isdigit. #include<stdio.h>
c) isalpha. int main () {
d) isxdigit. char s1[50] = "jack";
6. What does the following line print? char s2[50] = "jill";
isalpha ('$')? "$ is a ": "$ is not a ", "letter", char s3[50] = "";
printf("%c%s", toupper(s1[0]), &s1[1]);
$ is not a letter".
____________________________________
printf("%s", strcpy(s3, s2));
7. In the context of function strtok, a is a sequence of printf("%s", strcat(strcat(strcpy(s3, s1), " and "), s2));
token
characters separated by delimiters ______________.
printf("%zu", strlen(s1) + strlen(s2));
8. What is the maximum length of a C String. printf("%zu", strlen(s3)); // using s3 after part executes
_________________________?
Not a fxed lenght }
Output: ___________________________
jack
Question 2: [1x6 marks] [estimated time 6 minutes]
In the following questions mark True or False.
jill
Output: ___________________________
a) (T / F) The following definition initializes the
color array to the character string "blue": jack and jill
Output: ___________________________
Quiz 12 – Chapter 08 - Characters and Strings Page 1 of 2
CC-112 Programming Fundamentals Spring 2022
8
Output: ___________________________ Input string: If Life were predictable it would cease to be
life and be without the flavor.
13
Output: ___________________________ Search string: be
Expected Output: 2
2.
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <string.h>
#define SIZE1 25 int countOccurrences(char *text, char *search) {
int count = 0;
#define SIZE2 15 char *ptr = text;
int main(void) {
while ((ptr = strstr(ptr, search)) != NULL) {
char x[] = "Happy Birthday to You"; // initialize char count++;
array x ptr++; // Move pointer forward
}
char y[SIZE1] = ""; // create char array y
char z[SIZE2] = ""; // create char array z return count;
}
// copy contents of x into y
printf("%s%s\n%s%s\n", "The string in array x is: ", x, int main() {
"The string in array y is: ", strcpy(y, x)); char text[200] = "If Life were predictable it
would cease to be life and be without the
strncpy(z, x, SIZE2 - 1); // copy first 14 characters of x flavor.";
into z char search[] = "be";
z[SIZE2 - 1] = '\0'; // terminate string in z, because '\0'
not copied printf("Occurrences of \"%s\": %d\n", search,
countOccurrences(text, search));
printf("The string in array z is: %s\n", z); return 0;
} }
Output:
The string in array x is: Happy Birthday to You
_____________________________________________
The string in array y is: Happy Birthday to You
The string in array z is: Happy Birthday t
_____________________________________________
_____________________________________________
_____________________________________________
Question 4: [8 marks] [estimated time 10 minutes]
Write a C program that:
Write a program
that inputs several lines of text and a search string
and uses function strstr () to determine the total
occurrences of the search string in the lines of
text.
Print the result count
Quiz 12 – Chapter 08 - Characters and Strings Page 2 of 2