Docsity

Quiz 9 for Introduction C Programming | CMSC 106, Quizzes of Computer Science

Toggle sidebar
bg1
Name (printed):
Student ID #:
Section # (or TA’s:
name and time)
CMSC 106 Quiz #9 Fall 2007
1. [16 pts. total] Given the above code, give the output of the programm if we replace <some code> with
one of the followning: (if you think that the code has an error, write ”error”). Each is independent
- assume the results of one does not affect the results of the next question.
#include <stdio.h>
#include<string.h>
int main(){
char a[10]="abc";
char *b;
char c[50]="The cat with the hat";
<some code>
return 0;
}
a. [4 pts.] strncpy(a,"Jan",10);
printf("%s",a); Jan
b. [4 pts.] b = strrchr(c,’a’);
printf("%s %s",a,b); abc t with the hat
c. [4 pts.] strncat(c," is back.",25);
printf("%s",c); The cat with the hat is back
d. [4 pts.] c[7]=’\0’;
strncat(c," is black.",25);
printf("%s",c); The cat is black
pf2

Partial preview of the text

Download Quiz 9 for Introduction C Programming | CMSC 106 and more Quizzes Computer Science in PDF only on Docsity!

Name (printed):

Student ID #:

Section # (or TA’s: name and time)

CMSC 106 Quiz #9 Fall 2007

  1. [16 pts. total] Given the above code, give the output of the programm if we replace with one of the followning: (if you think that the code has an error, write ”error”). Each is independent
    • assume the results of one does not affect the results of the next question.

#include <stdio.h> #include<string.h> int main(){ char a[10]="abc"; char *b; char c[50]="The cat with the hat"; return 0; }

a. [4 pts.] strncpy(a,"Jan",10); printf("%s",a); Jan

b. [4 pts.] b = strrchr(c,’a’); printf("%s %s",a,b); abc t with the hat

c. [4 pts.] strncat(c," is back.",25); printf("%s",c); The cat with the hat is back

d. [4 pts.] c[7]=’\0’; strncat(c," is black.",25); printf("%s",c); The cat is black

  1. [14 pts] Write the complete function that takes in two strings (arrays of characters where you are sure the used portion terminates in a null character) as the only parameters and returns 0 for false or 1 for true which tells if all of the characters in the second string are present in the first. (Note: you can’t assume they will be present in that order, so you can’t use the strstr function.) (Also note: You do not need to be concerned with the number of times it appears in either string - just if it is present or not.) For example: if the first string is "Jandelyn" and the second string is "land" - the funciton should return true since all of those characters are indeed present. Anohter example: if the first string is "Jandelyn" and the second string is "deed" - the function should return true since all of those characters are indeed present (not we are ignoreing the quantity). One last example: if the first string is "Jandelyn" and the second string is "dear" - the funciton should return false since at least one of the characters is not present.

int contains(char full[], char list[]){ int listind = 0; while(list[listind] != ’\0’){ if(strchr(full, list[listind]) == NULL){ return 0; } else{ listind++; } } return 1; }