In this article, we will cover the basic array operations: insert, delete, and search. These operations allow us to modify and interact with arrays in C programming. Learn how to insert an element at any position, delete an element, and search for an element within an array using pointers. We will walk through examples, provide code snippets, and explain how each operation works.
Inserting an element into an array is a common operation where you add an element at a specific position. If the position exceeds the size of the array, we display an “Invalid Input” message. Here’s how to do it.
Array after insertion is:
CopyEdit1
2
3
10
4
5
#include <stdio.h>
#include <stdlib.h>
void insert(int n1, int *a, int l, int e) {
int i;
printf("Array after insertion is:\n");
for(i = 0; i < l - 1; i++) {
printf("%d\n", *(a + i));
}
printf("%d\n", e);
for(i = l - 1; i < n1; i++) {
printf("%d\n", *(a + i));
}
}
int main() {
int *a, n1, i, l, e;
scanf("%d", &n1);
a = (int*)malloc(n1 * sizeof(int));
for(i = 0; i < n1; i++) {
scanf("%d", a + i);
}
scanf("%d", &l);
if(l <= n1) {
scanf("%d", &e);
insert(n1, a, l, e);
} else {
printf("Invalid Input");
}
return 0;
}
When deleting an element from an array, you need to shift the remaining elements to the left after removal. If the position exceeds the array size, an “Invalid Input” message will be displayed.
Array after deletion is:
CopyEdit1
2
4
5
#include <stdio.h>
#include <stdlib.h>
void delete(int n, int *a, int l) {
int i, j;
if(l <= n) {
for(i = l - 1; i < n; i++) {
j = i + 1;
*(a + i) = *(a + j);
}
printf("Array after deletion is:\n");
for(i = 0; i < n - 1; i++) {
printf("%d\n", *(a + i));
}
} else {
printf("Invalid Input");
}
}
int main() {
int *a, n, i, l;
scanf("%d", &n);
a = (int*)malloc(sizeof(int) * n);
for(i = 0; i < n; i++) {
scanf("%d", (a + i));
}
scanf("%d", &l);
delete(n, a, l);
return 0;
}
The search operation checks if a given element exists in the array. If it does, it displays a message confirming the presence; otherwise, it informs the user that the element is absent.
cCopyEdit3 is present in the array
#include <stdio.h>
int i, l;
int search(int n, int *a, int m) {
for(i = 0; i < n; i++) {
if(m == a[i]) {
l = 1;
break;
}
}
if(l == 1) {
printf("%d is present in the array\n", m);
} else {
printf("%d is not present in the array\n", m);
}
}
int main() {
int n, m;
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &m);
search(n, a, m);
return 0;
}
Frequently asked questions in interviews
Q: Write a C program to reverse a given string without using the strrev()
function.
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[] = "Hello";
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
Explanation:
Q: Write a C program to check whether a given number is prime or not.
#include <stdio.h>
int isPrime(int num) {
if (num < 2) return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int num = 29;
if (isPrime(num))
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}
Explanation:
sqrt(num)
to optimize performance.Q: Write a program to swap two numbers without using a third variable.
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Explanation:
a
is set to a + b
, then b
gets a - b
, and finally, a
gets a - b
.Q: Write a program to find the factorial of a number using recursion.
#include <stdio.h>
long long factorial(int n) {
return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %lld\n", num, factorial(num));
return 0;
}
Explanation:
n! = n * (n-1)!
.Q: Write a program to find the largest element in an array.
#include <stdio.h>
int findLargest(int arr[], int size) {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
int main() {
int arr[] = {10, 20, 30, 25, 40};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Largest element: %d\n", findLargest(arr, size));
return 0;
}
Explanation:
Array operations like insertion, deletion, and searching are crucial for manipulating data structures in programming. In this tutorial, we walked through practical examples with detailed explanations and code snippets. By understanding these basic operations, you can easily handle arrays in C programming and build more complex data structures.