Chapter 04: Arrays and Strings in C
Course: Structured Programming Language (SPL) Course Code: 510201 Level: Beginner
Learning Objectives
- Define arrays and explain their advantages.
- Declare, initialize and access one-dimensional arrays.
- Process arrays using loops and understand two-dimensional arrays.
- Explain character arrays and null-terminated strings.
- Use common string functions safely.
1. What is an Array?
English Exam Definition: An array is a collection of elements of the same data type stored in contiguous memory locations and accessed using a common name and an index.
2. One-Dimensional Array
Declaration
data_type array_name[size];
Example:
int marks[5];
Initialization
int numbers[5] = {10, 20, 30, 40, 50};
int values[] = {2, 4, 6};
int data[5] = {1, 2}; /* remaining elements become 0 */
Access and Update
int numbers[3] = {10, 20, 30};
printf("%d", numbers[1]); /* 20 */
numbers[1] = 25;
3. Traversing an Array with a Loop
#include <stdio.h>
int main(void)
{
int marks[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter mark %d: ", i + 1);
if (scanf("%d", &marks[i]) != 1)
return 1;
}
printf("Marks: ");
for (i = 0; i < 5; i++)
{
printf("%d ", marks[i]);
}
return 0;
}
4. Solved Program: Sum and Average
#include <stdio.h>
int main(void)
{
int a[5];
int i;
long long sum = 0;
double average;
for (i = 0; i < 5; i++)
{
printf("Enter number %d: ", i + 1);
if (scanf("%d", &a[i]) != 1)
return 1;
sum += a[i];
}
average = (double)sum / 5.0;
printf("Sum = %lld\n", sum);
printf("Average = %.2f\n", average);
return 0;
}
Sample input: 10 20 30 40 50 Output:
Sum = 150
Average = 30.00
Dry Run
| i | a[i] | Sum before | Sum after |
|---|---|---|---|
| 0 | 10 | 0 | 10 |
| 1 | 20 | 10 | 30 |
| 2 | 30 | 30 | 60 |
| 3 | 40 | 60 | 100 |
| 4 | 50 | 100 | 150 |
5. Solved Program: Find the Largest Element
#include <stdio.h>
int main(void)
{
int a[5] = {12, 45, 7, 31, 28};
int largest = a[0];
int i;
for (i = 1; i < 5; i++)
{
if (a[i] > largest)
largest = a[i];
}
printf("Largest = %d\n", largest);
return 0;
}
Output: Largest = 45
6. Two-Dimensional Array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
| Column 0 | Column 1 | Column 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
Program: Add Two 2×2 Matrices
#include <stdio.h>
int main(void)
{
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2];
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
sum[i][j] = a[i][j] + b[i][j];
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
Output:
6 8
10 12
Program: Multiply Two 2×2 Matrices
#include <stdio.h>
int main(void)
{
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int product[2][2] = {{0, 0}, {0, 0}};
int i, j, k;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
for (k = 0; k < 2; k++)
product[i][j] += a[i][k] * b[k][j];
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
printf("%d ", product[i][j]);
printf("\n");
}
return 0;
}
Output:
19 22
43 50
7. Character Array and String
English Exam Definition: A string in C is a sequence of characters terminated by the null character ('\0').
char name[6] = "Rahat";
String Input and Output
char name[30];
scanf("%29s", name);
printf("%s\n", name);
#include <stdio.h>
int main(void)
{
char fullName[50];
printf("Enter your full name: ");
if (fgets(fullName, sizeof fullName, stdin) != NULL)
printf("Hello, %s", fullName);
return 0;
}
8. Common String Functions
| Function | — |
|---|---|
| strlen(s) | — |
| strcpy(dest, src) | — |
| strcat(dest, src) | — |
| strcmp(a, b) | — |
Program: String Length
#include <stdio.h>
#include <string.h>
int main(void)
{
char word[] = "Computer";
printf("Length = %zu\n", strlen(word));
return 0;
}
Output: Length = 8
Program: Compare Two Strings
#include <stdio.h>
#include <string.h>
int main(void)
{
char first[30] = "apple";
char second[30] = "apple";
if (strcmp(first, second) == 0)
printf("Strings are equal\n");
else
printf("Strings are different\n");
return 0;
}
9. Difference Between One-Dimensional and Two-Dimensional Arrays
| One-dimensional array | Two-dimensional array |
|---|---|
| — | — |
| — | — |
| int a[5]; | int a[2][3]; |
| a[2] | a[1][2] |
Part B: English Exam Notes
Q1. Define an array. An array is a collection of elements of the same data type stored in contiguous memory locations and accessed using a common name and index.
Q2. What is an array index? An array index is the integer position used to access an element. In C, array indexing starts at zero.
Q3. What is a two-dimensional array? A two-dimensional array is an array organized in rows and columns and accessed using two indices.
Q4. What is a string in C? A string is a sequence of characters terminated by the null character ('\0').
Q5. Why is string.h used? It provides declarations for common string-handling functions such as strlen(), strcpy(), strcat() and strcmp().
Short Questions (2 Marks)
- Write the syntax for declaring an integer array.
- What is the first index of an array in C?
- How many elements can int a[10] store?
- What is a two-dimensional array?
- What is the null character?
- Differentiate between a character and a string.
- Write the purpose of strlen() and strcmp().
- Why should gets() not be used?
Broad Questions (5 Marks)
- Explain array declaration, initialization and accessing with examples.
- Explain one-dimensional and two-dimensional arrays with examples.
- Write a C program to find the sum and average of five array elements.
- Define a string and explain common string-handling functions.
- Write a C program to compare two strings.
Programming Practice
- Read 10 integers and count how many are even.
- Find the smallest and largest values in an array.
- Reverse an array.
- Add two 3×3 matrices.
- Find the length of a string without using strlen().
- Count vowels in a string.
Common Mistakes
- Accessing a[size] instead of stopping at a[size - 1].
- Using an uninitialized array element.
- Forgetting that a C string needs space for '\0'.
- Using scanf("%s", ...) when the input contains spaces.
- Calling strcpy() or strcat() without checking destination capacity.
- Comparing strings with == instead of strcmp().
Quick Revision
- Array: same-type collection.
- Index: starts at 0.
- 1D array: one index.
- 2D array: row and column indices.
- String: character sequence ending with '\0'.
- strlen(): length.
- strcpy(): copy.
- strcat(): concatenate.
- strcmp(): compare.
NU Previous Questions and Board Question Archive
Questions appearing in readable NU paper copies
2019 - What is an array? Write its advantages and disadvantages. - Mention string-handling functions and explain them with examples. - Write a C program to multiply two matrices.
2022 - What is an array? Discuss its advantages and disadvantages. - What is a string? Name four string-handling functions and their actions. - Write a C program to check whether a string is a palindrome.
Topic-year references in the student-compiled NU question bank
- Definition of array: listed across 2007, 2008, 2011, 2012, 2017 and 2019.
- Array declaration / initialization: 2020 and 2021 topic tags appear in the bank.
- String definition: 2007, 2009, 2011, 2012, 2013, 2014, 2016, 2018, 2019, 2021 and 2022.
- String-handling functions: 2007, 2009, 2011, 2012, 2013, 2014, 2016, 2018, 2019, 2020, 2021 and 2022.
- Palindrome program: 2011 and 2022.
Selected exam reminder: String in C
Exam-ready English: A string in C is a sequence of characters terminated by the null character, written as the escape sequence \0.
Safe input reminder: A bounded scanf with a width limit reads one word only. Use fgets() for a line that may contain spaces. Never use gets().
Board and question-bank sources
- NU CSE Board Questions (2006–2018)
- NU CSE Board Questions (2019)
- NU CSE SPL Question Bank (510201)
- 2019 paper copy
- 2022 paper copy
Extra practice (not verified PYQs)
- Find the second-largest element in an integer array.
- Transpose a matrix.
- Count vowels, consonants, digits and spaces in a line of text.
Source caution: Question-bank year labels are secondary-source references. Newly written practice tasks are not presented as NU past questions.
Solved Board-Style Program: Check Whether a String Is a Palindrome
Problem: Read a word and check whether it reads the same from left to right and right to left.
#include <stdio.h>
#include <string.h>
int main(void)
{
char text[100];
int left, right;
int isPalindrome = 1;
printf("Enter a word: ");
if (scanf("%99s", text) != 1)
return 1;
left = 0;
right = (int)strlen(text) - 1;
while (left < right)
{
if (text[left] != text[right])
{
isPalindrome = 0;
break;
}
left++;
right--;
}
if (isPalindrome)
printf("Palindrome\n");
else
printf("Not a palindrome\n");
return 0;
}
Sample input: level
Output: Palindrome
Dry Run: level
| Comparison | Left character | Right character | Result |
|---|---|---|---|
| 1 | l | l | Match |
| 2 | e | e | Match |
| Stop | l | l | Middle reached |
Note: This beginner version reads one word and performs a case-sensitive comparison. Therefore, Level and a sentence containing spaces are not treated as the same palindrome as level. Handling spaces and letter case requires additional preprocessing.
Solved Program: Count Even Elements in an Array
#include <stdio.h>
int main(void)
{
int a[10];
int i, evenCount = 0;
for (i = 0; i < 10; i++)
{
printf("Enter number %d: ", i + 1);
if (scanf("%d", &a[i]) != 1)
return 1;
if (a[i] % 2 == 0)
evenCount++;
}
printf("Even elements = %d\n", evenCount);
return 0;
}
Sample input: 1 2 3 4 5 6 7 8 9 10 Output: Even elements = 5
Solved Program: Reverse an Array
#include <stdio.h>
int main(void)
{
int a[5] = {10, 20, 30, 40, 50};
int left = 0, right = 4, temp;
int i;
while (left < right)
{
temp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;
}
for (i = 0; i < 5; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
Output: 50 40 30 20 10
Exam point: Reversing an array in this way uses constant extra space because it swaps elements inside the original array.
End of Chapter 04