Assignment 6
Assignment 6
Assignment 6
Lab Exercise 06
Indian Institute of Technology, Patna
May 26, 2022
EXERCISE 1: Write a C program using a function to swap the values of two numbers. Pass the
numbers as the arguments of the function [not the references]. Print the numbers in main method
before calling the function and after calling the function. Inside the function print the numbers
before swapping and after swapping. Please note after taking the input numbers they will be printed
four times.
EXERCISE 2: Write a C program which reads three character strings str1, str2, and str3 of size 20, 4
and 5 respectively and display the strings. [You cannot use any string method from “string.h”]
a) The program checks whether str2 occurs in str1 as a substring and in that case displays the
starting index of all such occurrences.
b) The program replaces all occurrences of str2 in str1 by str3 and displays the newly formed string.
If that is not possible due to space limitation, then an appropriate message is printed.
EXERCISE 3: Write a C function void balance(int A[ ], int n, int k) which takes as parameter an array A
with n elements and does the following:
a) Finds out if the maximum of A[0] . . A[k − 1] is greater than the minimum of A[k] . . A[n − 1].
b) In case it is so, the function swaps the maxima and the minima and goes back to step (a).
Example run: Consider A = {12, 10, 20, 5, 6, 15} and the function call balance(A, 6, 3). A correct
execution of balance() should ensure that A = {6, 10, 5, 20, 12, 15} after the function returns.
EXERCISE 4: Write a C program to print a subset of an integer array such that the sum of all the
integers from that subset is equal to some integer k. Take the integers of the array and the value of k
from the user. If there is no subset like that then print an error message. At the end of your source
code write down the logic of your program as comments.