0% found this document useful (0 votes)
40 views20 pages

POINTERS

Uploaded by

2401200010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views20 pages

POINTERS

Uploaded by

2401200010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Software Development Fundamentals Lab – I [24B15CS111]

Assignment Sheet: Week 9 [30th Sep – 5th Oct, 2024]

(Topic: Pointers)

ENROLLMENT NUMBER-NJG246523
1.Write a C program using a pointer to declare a variable var with value 20 and print the value

of the ptr, var and *ptr.

CODE:-

#include <stdio.h>

int main()

int var = 20, *ptr;

ptr = &var;

printf("ptr=%p \nvar=%d\n*ptr=%d", ptr, var, *ptr);

return 0;

OUTPUT:-

2. WAP to find the length of a string using pointers.

CODE:-

#include <stdio.h>

int main()

{
char str[100];

printf("Enter the string: ");

scanf("%[^\n]%*c", str);

char *ptr = str;

int len = 0;

while (*ptr != '\0')

ptr++;

len++;

printf("length is = %d", len);

return 0;

OUTPUT:-

3. WAP to calculate the sum of elements of an array using pointers.

CODE:-

#include <stdio.h>

int main()

int size;

printf("enter the size of the array: ");

scanf("%d",&size);
int arr[size];

for (int i = 0; i < size; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);

int *ptr = arr;

int sum = 0;

for (int i = 0; i < size; i++)

sum = sum + *ptr;

ptr++;

printf("Sum of the array elements is %d", sum);

return 0;

OUTPUT:-
4. WAP to check if the number is prime or not using pointers.

CODE:-

#include <stdio.h>

#include<math.h>

int main()

int n;

int *p = &n;

printf("enter a number : ");

scanf("%d", p);

int isprime = 1;

for (int i = 2; i < *p; i++)

if (*p % i == 0)

isprime = 0;

break;

if (isprime == 1)

printf("%d is a prime number",n);

else
{

printf("%d is not a prime number",n);

return 0;

OUTPUT:-

5. WAP to reverse an array using pointers.

CODE:-

#include <stdio.h>

int main()

int size;

printf("enter the size of the array: ");

scanf("%d",&size);

int arr[size];

for (int i = 0; i < size; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);
}

int *p = arr;

int *q = &arr[size-1];

while (p < q)

int temp = *p;

*p = *q;

*q = temp;

p++;

q--;

printf("the reversed array is\n");

for (int i = 0; i < size; i++)

printf("%d ", arr[i]);

return 0;

OUTPUT:-
6. WAP to check if two strings are equal using pointers

CODE:-

#include <stdio.h>

void main()

char str1[50], str2[50];

char *p, *q;

printf("ENTER THE FIRST STRING: ");

scanf("%[^\n]%*c", str1);

printf("ENTER THE SECOND STRING: ");

scanf("%[^\n]%*c", str2);

p = str1;

q = str2;

int is_equal = 1;

while (*p != '\0' || *q != '\0')

{
if (*p != *q)

is_equal = 0;

break;

p++;

q++;

if (is_equal)

printf("THE STRINGS ARE EQUAL");

else

printf("THE STRINGS ARE UNEQUAL");

OUTPUT:-

7. Write a C program to find the kth smallest element in an integer array using pointers and

pointer arithmetic. Allow the user to input the array and the value of k. Assume k is valid (1
<= k <= array size).

CODE:-

#include <stdio.h>

int main()

int size;

printf("enter the size of the array: ");

scanf("%d",&size);

int arr[size];

for (int i = 0; i < size; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);

int k;

printf("Enter the value of k : ");

scanf("%d", &k);

if (k < 1 || k > size)

printf("Please enter the valid index");

int i, j;
int key;

for (i = 1; i < size; i++)

key = *(arr + i);

j = i - 1;

while (j >= 0 && arr[j] > key)

*(arr + j + 1) = *(arr + j);

j--;

*(arr + j + 1) = key;

printf("\n");

printf("sorted array ;-\n");

for (i = 0; i < size; i++)

{printf("%d ", arr[i]);}

printf("\n\nThe kth smallest element is : %d\n\n", *(arr + k - 1));

return 0;

OUTPUT:-
8. Write a C program to reverse a string using pointers. Allow the user to input a string, reverse

it in-place using pointers, and then print the reversed string.

CODE:-

#include <stdio.h>

#include <string.h>

int main() {

char arr[100];

printf("Enter the string: ");

scanf("%[^\n]%*c", arr);

int len = strlen(arr);

printf("Original string: ");

for (int i = 0; i < len; i++) {

printf("%c ", *(arr + i));

char temp;
for (int i = 0; i < len / 2; i++) {

temp = *(arr + i);

*(arr + i) = *(arr + len - (i + 1));

*(arr + len - (i + 1)) = temp;

printf("\nThis is the string after the reversal:\n");

for (int i = 0; i < len; i++) {

printf("%c ", *(arr + i));

return 0;

OUTPUT:-

9. Write a C program that takes a 2D integer array as input from the user. Assign an integer

pointer to the same. Then after that, calculate and print the sum of all the elements in the

array.

CODE:-

#include <stdio.h>
int main()

printf("Enter the number of rows and columns : ");

int r, c;

scanf("%d %d", &r, &c);

int arr[r][c];

int *ptr;

int i, j;

for (i = 0; i < r; i++)

for (j = 0; j < c; j++)

printf("\nEnter the element for position (%d,%d):", i, j);

scanf("%d", (*(arr + i) + j));

int sum = 0;

int *sPointer = &sum;

for (i = 0; i < r; i++)

for (j = 0; j < c; j++)

*sPointer = *sPointer + *(*(arr + i) + j);

}
}

printf("\n\nThe sum of all the elements in the array is %d", sum);

return 0;

OUTPUT:-

10.Write a C program that checks if a given string is a palindrome (reads the same forwards and

backwards) using pointers and pointer arithmetic. Allow the user to input a string.

CODE:-

#include <stdio.h>

#include <string.h>

int main() {
char str[100];

printf("Enter the string: ");

scanf("%[^\n]%*c", str);

int len = strlen(str);

char *start = str;

char *end = str + len - 1;

int isPalindrome = 1;

while (start < end) {

if (*start != *end) {

isPalindrome = 0;

break;

start++;

end--;

if (isPalindrome) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");

return 0;
}

OUTPUT:-

11.Write a C program that counts and prints the number of vowels in a given string using

pointers and pointer arithmetic. Allow the user to input a string.

CODE:-

#include <stdio.h>

int main()

char *s;

char str[20];

int c = 0;

printf("ENTER THE STRING: ");

scanf("%[^\n]%*c", str);

s = &str[0];

for (; *s != '\0'; s++)

if (*s == 'A' || *s == 'E' || *s == 'I' || *s == '0' || *s == 'U' || *s == 'a' || *s == 'e' || *s == 'i'


|| *s == 'o' || *s == 'u')

c = c + 1;

}
printf("THE TOTAL NUMBER OF VOWELS = %d", c);

return 0;

OUTPUT:-

12.Write a program to print the elements of the array using dynamic memory allocation in C.

Check if the memory has been successfully allocated by malloc or not.

CODE:-

#include <stdio.h>

#include <stdlib.h>

int main() {

int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

int *arr = (int *)malloc(size * sizeof(int));

for (int i = 0; i < size; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);

printf("Array elements are:\n");


for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

free(arr);

return 0;

OUTPUT:-

13.Write a program to read a one-dimensional array, print sum of all elements along with

inputted array elements using Dynamic Memory Allocation.

CODE:-

#include <stdio.h>

#include <stdlib.h>

int main() {
int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

int *arr = (int *)malloc(size * sizeof(int));

if (arr == NULL) {

printf("Memory allocation failed.\n");

return 1;

int sum = 0;

for (int i = 0; i < size; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);

sum += arr[i];

printf("Array elements are:\n");

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");
printf("Sum of all elements: %d\n", sum);

free(arr);

return 0;

OUTPUT:-

You might also like