Skip to content
Unit 3 Syllabus Note· Programming with C Theory

Programming with C

Comprehensive exam revision notes for BCA-103N Unit 3 (Arrays, 2D Arrays, Strings & String Handling Functions). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.

Programming with C

Arrays, 2D Arrays, Strings & String Handling Functions

Core Definitions & Key Concepts

An Array in C

An array is a homogeneous collection of elements of the same data type stored in contiguous physical memory locations, referenced by a common name and a zero-based integer index/subscript.

What is the Base Address of an array

The memory address of the very first element (&arr[0]) of the array. In C, the array name itself decays into a pointer to its base address.

State the memory address calculation formula for an element arr[i] in a 1D array

Address(arr[i]) = Base_Address + (i * sizeof(element_type))

What happens if you access an array out of its bounds in C (e.g., arr[10] in an array of size 5)

C does not perform run-time array bounds checking. Accessing out-of-bounds memory triggers Undefined Behavior, resulting in data corruption, segmentation faults, or silent security bugs.

How are 2D arrays organized in memory: Row-Major vs Column-Major

C strictly employs Row-Major Order: all elements of row 0 are stored contiguously in memory, followed immediately by row 1, row 2, and so on.

State the address calculation formula for element arr[i][j] in a 2D array in Row-Major order

Address(arr[i][j]) = Base_Address + [(i * Total_Columns) + j] * sizeof(element_type)

How is a String represented internally in C

A string in C is a sequence of characters stored in a contiguous 1D char array terminated by an invisible null character ('\0', ASCII value 0).

What is the purpose of the null character ('\0') in strings

It marks the logical end of the string in memory. Standard string library functions (strlen, printf, strcpy) rely on '\0' to know when to stop reading bytes.

What is the limitation of scanf("%s", str) when reading strings

scanf("%s") stops reading at the first whitespace character (space, tab, newline). To read full strings containing spaces, fgets(str, sizeof(str), stdin) must be used.

What does strcmp(str1, str2) return

Returns 0 if strings are identical; a negative integer if str1 is lexicographically less than str2; a positive integer if str1 is lexicographically greater than str2.

Conceptual & Architectural Analysis

1D Array Declaration, Initialization, and Memory Layout

C
1int marks[5] = {85, 92, 78, 64, 90}; // Compile-time initialization

Memory Layout: If base address is 2000 and sizeof(int) = 4:

  • marks[0] -> Address 2000 (Value: 85)
  • marks[1] -> Address 2004 (Value: 92)
  • marks[2] -> Address 2008 (Value: 78)
  • marks[3] -> Address 2012 (Value: 64)
  • marks[4] -> Address 2016 (Value: 90)

Total bytes allocated = 5 * 4 = 20 bytes.

Write a C program to perform Matrix Addition and Transpose on 2D arrays

C
1#include <stdio.h>
2
3#define MAX 10
4
5int main(void) {
6 int A[MAX][MAX], B[MAX][MAX], Sum[MAX][MAX], Trans[MAX][MAX];
7 int r, c, i, j;
8
9 printf("Enter rows and columns (e.g. 3 3): ");
10 scanf("%d %d", &r, &c);
11
12 printf("Enter Matrix A elements:\n");
13 for (i = 0; i < r; i++)
14 for (j = 0; j < c; j++)
15 scanf("%d", &A[i][j]);
16
17 printf("Enter Matrix B elements:\n");
18 for (i = 0; i < r; i++)
19 for (j = 0; j < c; j++)
20 scanf("%d", &B[i][j]);
21
22 // Addition and Transpose
23 for (i = 0; i < r; i++) {
24 for (j = 0; j < c; j++) {
25 Sum[i][j] = A[i][j] + B[i][j];
26 Trans[j][i] = A[i][j]; // Swap row and column indices
27 }
28 }
29
30 printf("\n--- Matrix A + B ---\n");
31 for (i = 0; i < r; i++) {
32 for (j = 0; j < c; j++) printf("%4d", Sum[i][j]);
33 printf("\n");
34 }
35
36 printf("\n--- Transpose of Matrix A ---\n");
37 for (i = 0; i < c; i++) {
38 for (j = 0; j < r; j++) printf("%4d", Trans[i][j]);
39 printf("\n");
40 }
41 return 0;
42}

Five important string manipulation functions in <string.h> with syntax and examples

  1. size_t strlen(const char *s): Returns character count excluding null terminator. Example: strlen("BCA") returns 3.
  2. char *strcpy(char *dest, const char *src): Copies source string into destination buffer (including '\0').
  3. char *strcat(char *dest, const char *src): Appends source string onto the end of destination string.
  4. int strcmp(const char *s1, const char *s2): Compares two strings lexicographically by character ASCII values.
  5. char *strstr(const char *haystack, const char *needle): Finds first occurrence of substring in string.

Write a C program to implement Linear Search and Bubble Sort on a 1D array

C
1#include <stdio.h>
2
3void bubble_sort(int arr[], int n) {
4 for (int i = 0; i < n - 1; i++) {
5 for (int j = 0; j < n - i - 1; j++) {
6 if (arr[j] > arr[j + 1]) {
7 int temp = arr[j];
8 arr[j] = arr[j + 1];
9 arr[j + 1] = temp;
10 }
11 }
12 }
13}
14
15int linear_search(int arr[], int n, int key) {
16 for (int i = 0; i < n; i++) {
17 if (arr[i] == key) return i; // Found at index i
18 }
19 return -1; // Not found
20}

How Character Arrays differ from Numeric Arrays in C

  1. Termination: Character strings MUST be terminated with null character '\0'; numeric arrays have no termination marker and require explicit size tracking.
  2. I/O Support: Character arrays can be printed directly as a single entity with %s or puts(); numeric arrays must be printed element-by-element using loops.
  3. String Literals: Character arrays can be initialized with string literals: char name[] = "MJPRU"; which automatically appends '\0'.

In-Depth Analysis & Implementation

Write a complete C program to perform Matrix Multiplication of two 2D matrices after validating conformability conditions

C
1/**
2 * Program: 2D Matrix Multiplication with Conformability Check
3 * Condition: Matrix A (r1 x c1) * Matrix B (r2 x c2) requires c1 == r2
4 * Resulting Matrix: C (r1 x c2)
5 */
6#include <stdio.h>
7
8#define MAX 10
9
10int main(void) {
11 int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
12 int r1, c1, r2, c2;
13 int i, j, k;
14
15 printf("Enter dimensions of Matrix A (rows cols): ");
16 scanf("%d %d", &r1, &c1);
17 printf("Enter dimensions of Matrix B (rows cols): ");
18 scanf("%d %d", &r2, &c2);
19
20 // Conformability validation
21 if (c1 != r2) {
22 printf("Error: Multiplication not conformable! Columns of A (%d) != Rows of B (%d).\n", c1, r2);
23 return 1;
24 }
25
26 printf("Enter elements of Matrix A:\n");
27 for (i = 0; i < r1; i++)
28 for (j = 0; j < c1; j++)
29 scanf("%d", &A[i][j]);
30
31 printf("Enter elements of Matrix B:\n");
32 for (i = 0; i < r2; i++)
33 for (j = 0; j < c2; j++)
34 scanf("%d", &B[i][j]);
35
36 // Initialize product matrix C with zeros
37 for (i = 0; i < r1; i++)
38 for (j = 0; j < c2; j++)
39 C[i][j] = 0;
40
41 // Matrix multiplication: C[i][j] = sum(A[i][k] * B[k][j])
42 for (i = 0; i < r1; i++) {
43 for (j = 0; j < c2; j++) {
44 for (k = 0; k < c1; k++) {
45 C[i][j] += A[i][k] * B[k][j];
46 }
47 }
48 }
49
50 printf("\n--- Resultant Matrix C (%d x %d) ---\n", r1, c2);
51 for (i = 0; i < r1; i++) {
52 for (j = 0; j < c2; j++) {
53 printf("%6d", C[i][j]);
54 }
55 printf("\n");
56 }
57 return 0;
58}

Write custom C implementations of strlen(), strcpy(), strcat(), strcmp(), and strrev() without using <string.h>

C
1#include <stdio.h>
2
3// 1. Custom string length
4int my_strlen(const char str[]) {
5 int len = 0;
6 while (str[len] != '\0') len++;
7 return len;
8}
9
10// 2. Custom string copy
11void my_strcpy(char dest[], const char src[]) {
12 int i = 0;
13 while (src[i] != '\0') {
14 dest[i] = src[i];
15 i++;
16 }
17 dest[i] = '\0'; // Crucial null termination
18}
19
20// 3. Custom string concatenation
21void my_strcat(char dest[], const char src[]) {
22 int i = 0, j = 0;
23 while (dest[i] != '\0') i++; // Find end of dest
24 while (src[j] != '\0') {
25 dest[i++] = src[j++];
26 }
27 dest[i] = '\0';
28}
29
30// 4. Custom string compare
31int my_strcmp(const char s1[], const char s2[]) {
32 int i = 0;
33 while (s1[i] != '\0' && s1[i] == s2[i]) i++;
34 return (unsigned char)s1[i] - (unsigned char)s2[i];
35}
36
37// 5. Custom string reverse in-place
38void my_strrev(char str[]) {
39 int i = 0, j = my_strlen(str) - 1;
40 while (i < j) {
41 char temp = str[i];
42 str[i] = str[j];
43 str[j] = temp;
44 i++; j--;
45 }
46}

Write a C program to check whether a given string is a Palindrome ignoring case and whitespace

C
1#include <stdio.h>
2#include <stdbool.h>
3#include <ctype.h>
4#include <string.h>
5
6int main(void) {
7 char str[100];
8 char cleaned[100];
9 int i, k = 0;
10
11 printf("Enter a string: ");
12 fgets(str, sizeof(str), stdin);
13
14 // Remove trailing newline if present
15 str[strcspn(str, "\n")] = '\0';
16
17 // Clean: keep only alphanumeric characters converted to lowercase
18 for (i = 0; str[i] != '\0'; i++) {
19 if (isalnum((unsigned char)str[i])) {
20 cleaned[k++] = tolower((unsigned char)str[i]);
21 }
22 }
23 cleaned[k] = '\0';
24
25 // Check palindrome using two-pointer technique
26 int left = 0, right = k - 1;
27 bool is_palindrome = true;
28 while (left < right) {
29 if (cleaned[left] != cleaned[right]) {
30 is_palindrome = false;
31 break;
32 }
33 left++;
34 right--;
35 }
36
37 if (is_palindrome)
38 printf("\"%s\" IS a valid Palindrome!\n", str);
39 else
40 printf("\"%s\" is NOT a palindrome.\n", str);
41
42 return 0;
43}

Important Exam Points & Quick Revision

In BCA-103N Unit 3, Matrix Multiplication and writing string manipulation functions without using <string.h> are frequent 15-mark questions. Always include null termination '\0' in string code.

Your reading progress
0%

100% left on this page — finishing it closes the gap for Arrays, 2D Arrays, Strings & String Handling Functions.

Weak topic hint

Arrays, 2D Arrays, Strings & String Handling Functions still counts as a weak topic until you finish this note and answer its questions.

Practise this unit
What Should You Do Next?

Test Your Understanding

Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.