Assignment 9 PPS
Problem 1: Write a C program to read two numbers from user and add them using pointers.
sol.
#include<stdio.h>
// Function for add two numbers using pointers
void addNumbers(int *num1, int *num2, int *sum) {
*sum = *num1 + *num2;
}
int main() {
int num1, num2, sum;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Calling the sum function
addNumbers(&num1, &num2, &sum);
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
Problem 2: Write a C program to swap two elements using the concept of pointers. Create a function swap and use call by reference.
sol.
#include<stdio.h>
// Function for swapping
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
//before swapping
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2); // Calling swap function
//After swapping
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Problem 3: Write a C program to input elements in an array and print array using pointers.
sol.
#include<stdio.h>
// Function for inputing elements into an array
void inputArray(int *arr, int size) {
int i;
printf("Enter %d elements:\n", size);
for (i = 0; i < size; i++) {
scanf("%d", arr + i); // Using pointer arithmetic to access array elements
}
}
// Function for printing elements of an array
void printArray(int *arr, int size) {
int i;
printf("The array elements are:\n");
for (i = 0; i < size; i++) {
printf("%d ", *(arr + i)); // Using pointer arithmetic to access array elements
}
printf("\n");
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size]; // Declaring array
inputArray(arr, size); //Calling function for Inputing elements
printArray(arr, size); //Calling function for Print elements of the array
return 0;
}
Problem 4: Write a C program to search an element in array using pointers.
sol.
#include<stdio.h>
int searchElement(int *arr, int size, int key) {
int i;
for (i = 0; i < size; i++) {
if (*(arr + i) == key)
return i;
}
return -1;
}
int main() {
int size, key,i;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: "); scanf("%d", &key);
int index = searchElement(arr, size, key);
if (index != -1)
printf("Element %d found at index %d\n", key, index);
else
printf("Element %d not found in the array\n", key);
return 0;
}
Problem 5: Write a C program to concatenate two strings in a single string using pointers.
sol.
#include<stdio.h>
#include<string.h>
// Function to concatenate two strings
void concatenateStrings(char *str1, char *str2, char *result) {
while (*str1 != '\0') { // Copying the first string into result
*result = *str1;
result++;
str1++;
}
while (*str2 != '\0') { // Copying the second string into result
*result = *str2;
result++;
str2++;
}
*result = '\0'; // Terminate the concatenated string
}
int main() {
char str1[100], str2[100], result[200];
puts("Enter the first string: "); gets(str1);
puts("Enter the second string: "); gets(str2);
concatenateStrings(str1, str2, result); // Calling the concatenateStrings function
printf("Concatenated string: %s\n", result); // Printing the concatenated string
return 0;
}
Problem 6: Write a C program to find reverse of a string using pointers.
sol.
#include<stdio.h>
#include<string.h>
// Function to reverse a string using pointers
void reverseString(char *str) {
int length = strlen(str); // Finding the length of the string
char *start = str; // Pointer to the start of the string
char *end = str + length - 1; // Pointer to the end of the string
// Swap characters from start to end
while (start < end) {
// Swap characters
char temp = *start;
*start = *end;
*end = temp;
// Move pointers
start++;
end--;
}
}
int main() {
char str[100];
puts("Enter a string: ");
gets(str);
reverseString(str); // Calling the reverseString function
printf("Reversed string: %s\n", str); // Printing the reversed string
return 0;
}
0 comments:
Post a Comment