Starting from:

$30

CS2211- Lab 8 Solved

The objective of this lab is to practice:  o Strings

If you would like to leave, and at least 30 minutes have passed, raise your hand and wait for the TA. 

Show the TA what you did. If, and only if, you did a reasonable effort during the lab, he/she will give you the lab mark. 

==========================================================================

In this lab, you should decide on the correct responses before running the code to test the real result.

1. Type and execute the following C program.  

Explain why the output of the last two printf statements is not the same.

 

#include <stdio.h

 

int main(void)

{

  char a[10] = {0}, b[10] = {0};

 

  strcpy(a,"ABCDEFGHI");   a[4] = 0;   strcpy(b, a);

 

  printf("%s\n", a);   printf("%s\n", b);

 

  printf("\n");   a[4] = 'X';   b[4] = 'X';

 

  printf("%s\n", a);   printf("%s\n", b);

 

  return 0;

}

2.   char s[] = "abc def", *p; p = s;

printf("%d\n", *p);

a)  abc def b) abc  c) the ASCII code of letter 'a' d) Random Garbage

3.   char s[] = "abc def"; int * i; i = s;

printf("%d\n", *i);

a)  abc def b) abc  c) the ASCII code of letter 'a' d) Random Garbage

4.   int a = 3, *p; p = &a;

Copyright © 2015 Mahmoud El-Sakka. 

 

printf("%d\n", p);

a)  3        b) the ASCII code of letter '3' c) Won’t run d) Random Garbage

5.   int a; int * p; char b; a = 65; p = &a; b = *p; printf("%d\n", b);

a)  65       b) A       c) Won’t Run    d) Random Garbage

6.   int a = 3; int * p = &a; int **q = &p; int ***r = &q; **q = 4;

printf("%d %d\n", *p, ***r);

a)  4 3      b) 3 4  c) 4 4 d) 3 3     e) Won’t Run

7.   Descripe the array that is defined in the following C statement. Indicate what is are  assigned to the indiviuale array elemets.

char abc[2][3][4] = { { {'a', 'b', 'c'}, {'d', 'e'}, {'f'}},  

                      { {'g'},{},{'h','i'}}}; 

What is the output of

printf("%s\n", abc[0][0]); printf("%s\n", abc[0][1]); printf("%s\n", abc[0][2]); printf("%s\n", abc[1][0]); printf("%s\n", abc[1][1]); printf("%s\n", abc[1][2]);

8.   A C program contains the following declaration.

static char *color[6] = {"Red", "Green", "Blue", "White", "black", "Yellow");

a)  What is the meaning of color?

b)  What is the value of *color?

c)  What is the meaning of (color + 2)?

d)  What is the value of *(color + 2)?

e)  How do color[5] and *(color + 5) differ?

 

9.   Write a prototype for a function that accepts an argument which is a pointer to an integer array and return a pointer to a character.

10. What is the different between int *a[] and int (*a)[]

11. Write a program that takes in an undefined number of words from the user and then merges all of the words letter by letter. For example if the program is called merge we could type on the command line:  merge Aaaaa Bbb Cccc

and the resulting output would be: AaaaaBbbCccc

More products