ArraysString
ArraysString
Definition of an Array in C:
An array is a collection of variables of the same type, stored in contiguous memory locations. It is a
data structure that allows storing multiple elements under a single name, making it easier to manage
large amounts of data.
data_type array_name[array_size];
3. Types of Arrays in C:
• Example Syntax:
int arr[5];
#include <stdio.h>
int main() {
return 0;
A two-dimensional array can be thought of as an array of arrays, commonly used for matrices.
• Example Syntax:
int arr[3][3];
Example Program:
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("Elements of 2D array:\n");
printf("\n");
return 0;
c) Multidimensional Arrays:
You can have arrays with more than two dimensions (3D, 4D, etc.), though it's not commonly used.
• int arr[2][3][4];
#include <stdio.h>
int main() {
int arr[2][3][4] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
},
};
printf("Elements of 3D array:\n");
return 0;
d) String Array:
In C, a string is an array of characters. A string ends with a special null character '\0' to mark the end
of the string.
#include <stdio.h>
int main() {
return 0;
Dynamic memory allocation allows you to allocate memory at runtime using functions from the
stdlib.h library. It is mainly used when you don't know the exact size of the array in advance.
a) malloc() Function:
Example Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int* arr = (int*) malloc(5 * sizeof(int)); // Dynamically allocate memory for 5 integers
if (arr == NULL) {
return 1;
// Initialize array
free(arr);
return 0;
b) calloc() Function:
The calloc() function allocates memory for an array of elements and initializes the memory to zero.
c) realloc() Function:
d) free() Function:
free(ptr);
• Answer: An array is a collection of elements of the same data type stored in contiguous
memory locations. It is used to store multiple values in a single variable.
Q2: What is the difference between malloc and calloc in C?
• Answer: malloc() allocates memory without initializing it, while calloc() allocates memory
and initializes it to zero.
• Answer: No, the size of a statically declared array cannot be changed during program
execution. To change the size dynamically, you must use dynamic memory allocation
functions like malloc() or realloc().
• Answer: A string is an array of characters in C, ending with a null character '\0' to indicate
the end of the string.
#include <stdio.h>
int main() {
scanf("%d", &A[i][j]);
scanf("%d", &B[i][j]);
// Matrix Addition
printf("\n");
return 0;
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
A[2][0]: 7
A[2][1]: 8
A[2][2]: 9
B[0][0]: 9
B[0][1]: 8
B[0][2]: 7
B[1][0]: 6
B[1][1]: 5
B[1][2]: 4
B[2][0]: 3
B[2][1]: 2
B[2][2]: 1
Matrix A + Matrix B =
10 10 10
10 10 10
10 10 10
Matrix multiplication involves multiplying rows of the first matrix with columns of the second
matrix.
#include <stdio.h>
#define MAX 3
int main() {
scanf("%d", &A[i][j]);
scanf("%d", &B[i][j]);
// Matrix Multiplication
printf("\n");
return 0;
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
A[2][0]: 7
A[2][1]: 8
A[2][2]: 9
B[0][0]: 9
B[0][1]: 8
B[0][2]: 7
B[1][0]: 6
B[1][1]: 5
B[1][2]: 4
B[2][0]: 3
B[2][1]: 2
B[2][2]: 1
Matrix A * Matrix B =
30 24 18
84 69 54
138 114 90
Matrix transpose involves swapping the rows and columns of the matrix.
#include <stdio.h>
#define MAX 3
int main() {
scanf("%d", &matrix[i][j]);
// Matrix Transpose
transpose[j][i] = matrix[i][j];
printf("\nTransposed Matrix:\n");
printf("\n");
return 0;
Matrix[0][0]: 1
Matrix[0][1]: 2
Matrix[0][2]: 3
Matrix[1][0]: 4
Matrix[1][1]: 5
Matrix[1][2]: 6
Matrix[2][0]: 7
Matrix[2][1]: 8
Matrix[2][2]: 9
Transposed Matrix:
147
258
369
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
char temp;
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
int main() {
char str[100];
reverseString(str);
return 0;
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
int main() {
int num;
scanf("%d", &num);
return 0;
Enter a number: 5
This program demonstrates how to perform a linear search on a one-dimensional array to find an
element.
#include <stdio.h>
if (arr[i] == target) {
int main() {
scanf("%d", &target);
if (result != -1) {
} else {
}
return 0;
Binary search works only on a sorted array and is much faster than linear search. Here’s an
example program for binary search.
#include <stdio.h>
if (arr[mid] == target) {
low = mid + 1;
} else {
high = mid - 1;
int main() {
int arr[] = {10, 20, 30, 40, 50};
scanf("%d", &target);
if (result != -1) {
} else {
return 0;
Bubble sort is a simple sorting algorithm that repeatedly steps through the list to compare
adjacent elements and swap them if necessary.
#include <stdio.h>
arr[j] = arr[j+1];
arr[j+1] = temp;
printf("\n");
int main() {
printArray(arr, size);
bubbleSort(arr, size);
printArray(arr, size);
return 0;
}
Unsorted array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
This program calculates the length of a string manually without using the built-in strlen() function.
#include <stdio.h>
int length = 0;
length++;
return length;
int main() {
char str[100];
return 0;
}
Enter a string: Hello, World!
This program checks whether a string is a palindrome, i.e., it reads the same forwards and
backwards.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
if (tolower(str[start]) != tolower(str[end])) {
start++;
end--;
return 1; // Palindrome
int main() {
char str[100];
} else {
return 0;
#include <stdio.h>
#define MAX 3
int main() {
scanf("%d", &A[i][j]);
}
// Input for Matrix B
scanf("%d", &B[i][j]);
// Matrix Multiplication
printf("\n");
return 0;
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
A[2][0]: 7
A[2][1]: 8
A[2][2]: 9
B[0][0]: 1
B[0][1]: 2
B[0][2]: 3
B[1][0]: 4
B[1][1]: 5
B[1][2]: 6
B[2][0]: 7
B[2][1]: 8
B[2][2]: 9
Matrix A * Matrix B =
30 36 42
66 81 96
#include <stdio.h>
max = arr[i];
return max;
int main() {
return 0;
This program sorts an array of strings in alphabetical order using the Bubble Sort algorithm.
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 5
strcpy(temp, arr[j]);
int main() {
char arr[MAX_STRINGS][MAX_LENGTH];
bubbleSort(arr, MAX_STRINGS);
printf("%s\n", arr[i]);
return 0;
}
Enter 5 strings:
apple
banana
grape
orange
kiwi
apple
banana
grape
kiwi
orange
This program counts the number of vowels in each string of an array of strings.
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 5
int count = 0;
char c = str[i];
count++;
}
return count;
int main() {
char arr[MAX_STRINGS][MAX_LENGTH];
return 0;
Enter 5 strings:
apple
banana
orange
grape
kiwi
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 5
int start = 0;
char temp;
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
int main() {
char arr[MAX_STRINGS][MAX_LENGTH];
reverseString(arr[i]);
return 0;
Enter 5 strings:
hello
world
apple
banana
kiwi
This program demonstrates how to concatenate two arrays of strings into a single array.
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 5
int main() {
int k = 0;
strcpy(result[k], arr1[i]);
k++;
k++;
printf("%s\n", result[i]);
return 0;
hello
world
apple
banana
kiwi
cat
dog
elephant
tiger
lion
hello
world
apple
banana
kiwi
cat
dog
elephant
tiger
lion
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 5
int main() {
char arr[MAX_STRINGS][MAX_LENGTH];
char longest[MAX_LENGTH];
int maxLength = 0;
maxLength = len;
strcpy(longest, arr[i]);
}
return 0;
Enter 5 strings:
hello
world
applepie
banana
kiwi
This program compares two arrays of strings and checks if they are identical.
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 5
if (strcmp(arr1[i], arr2[i]) != 0) {
int main() {
if (compareStrings(arr1, arr2)) {
} else {
return 0;
apple
banana
cherry
date
fig
apple
banana
cherry
date
fig