Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CS1200 Spring 2018

Assignment 5
Total number of questions: 3
Total points: 30

Submission Guideline: Write your answer in a word document and submit to e-learning.

(10pts) Q1. Find the error in each of the following program segments and explain how to
correct it:
a) char s[10];
strncpy(s, “hello”, 5);
printf(“%s\n”, s);
b) printf(“%s”, ‘a’);
c) char s[12];
strcpy(s, “Welcome Home”);
d) if (strcmp(string1, string2)) {
printf(“The strings are equal\n”);
}

(10pts) Q2. What does this program do? And explain your answer.
#include <stdio.h>

void mystery1 (char *, const char *); /* prototype */

int main(void)
{
char string1[80], string2[80]; /* create char array */
printf ("Enter two strings : ");
scanf ("%s%s", string1, string2);
mystery1 (string1, string2);
printf ("%s\n", string1);
return 0; /* indicates successful termination */
}

/* What does this function do? */


void mystery1 (char *s1, const char *s2)
{
while (*s1 != '\0') {
++s1;
} /* end while */
for ( ;*s1 = *s2; s1++, s2++) {
; /* empty statement */
} /* end for */
} /* end function mystery1 */
(10pts) Q3. What does this program do? And explain your answer.
#include <stdio.h>

int mystery2 (const char *); /* prototype */

int main(void)
{
char string[80]; /* create char array */
printf ("Enter a string: ");
scanf ("%s", string);
printf ("%d\n", mystery2 (string));
return 0; /* indicates successful termination */
}

/* What does this function do? */


int mystery2 (const char *s)
{
int x; /* counter */

/* loop through string */


for (x = 0; *s != '\0'; s++) {
++x;
} /* end for */
return x;
} /* end function mystery2 */

You might also like