0% found this document useful (0 votes)
3 views43 pages

Cpp

The document contains multiple C programming problem statements and their solutions, primarily focused on matrix operations, string manipulation, and user-defined functions. Each problem includes objectives, sample code, and expected outputs, demonstrating various programming concepts. The problems range from checking upper triangular matrices to string concatenation and vowel replacement.

Uploaded by

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

Cpp

The document contains multiple C programming problem statements and their solutions, primarily focused on matrix operations, string manipulation, and user-defined functions. Each problem includes objectives, sample code, and expected outputs, demonstrating various programming concepts. The problems range from checking upper triangular matrices to string concatenation and vowel replacement.

Uploaded by

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

Name : Aryan Chauhan Class Roll no : 12

Section: D1 Course : BCA 2nd Sem

Problem Statement 01 : Develop a C program to test whether a given matrix of order M


× N is an upper triangular matrix or not. An upper triangular matrix is the one in which all the
elements below its principal diagonal are zero.

Objective : The objective of this C program is to determine whether a given square


matrix (of order M × N) is an upper triangular matrix or not.

Solution:
#include <stdio.h>

int main() {

int M, N;

int isUpper = 1;

printf("Enter number of rows (M): ");

scanf("%d", &M);
printf("Enter number of columns (N): ");

scanf("%d", &N);

if (M != N) {

printf("Matrix is not square. Upper triangular matrix must be square.\n");

return 0;

int matrix[M][N];

printf("Enter elements of the matrix:\n");


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

for (int j = 0; j < N; j++) {

printf("Element [%d][%d]: ", i, j);

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

for (int i = 1; i < M; i++) {


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

if (matrix[i][j] != 0) {
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
isUpper = 0;

break;
}

if (!isUpper)

break;

// Result
if (isUpper)

printf("The matrix is an upper triangular matrix.\n");

else

printf("The matrix is NOT an upper triangular matrix.\n");

return 0;

Output :
Enter number of rows (M): 3
Enter number of columns (N): 3

Enter elements:

123

056

009

The matrix is an upper triangular matrix.


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 02 : Write a C program to read two square matrices of non-zero


elements of order M X M and then subtract the elements of the second matrix from the first
matrix. Then display the resultant matrix to the console.

Objective : A c program that reads two square matrices of order M × M, subtracts the
second matrix from the first, and displays the resultant matrix.

Solution :
#include <stdio.h>

int main() {

int M;

printf("Enter the order of the square matrices (M): ");

scanf("%d", &M);

int matrix1[M][M], matrix2[M][M], result[M][M];


printf("\nEnter elements of the first matrix (%d x %d):\n", M, M);

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

for (int j = 0; j < M; j++) {

printf("matrix1[%d][%d]: ", i, j);

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

printf("\nEnter elements of the second matrix (%d x %d):\n", M, M);


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

for (int j = 0; j < M; j++) {

printf("matrix2[%d][%d]: ", i, j);

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

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


for (int j = 0; j < M; j++) {

result[i][j] = matrix1[i][j] - matrix2[i][j];


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
}

}
printf("\nResultant Matrix after Subtraction (matrix1 - matrix2):\n");

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

for (int j = 0; j < M; j++) {

printf("%d\t", result[i][j]);

printf("\n");

return 0;

Output :
Enter the order of the square matrices (M): 2

Enter elements of the first matrix:

matrix1[0][0]: 5

matrix1[0][1]: 7

matrix1[1][0]: 3
matrix1[1][1]: 9

Enter elements of the second matrix:

matrix2[0][0]: 2

matrix2[0][1]: 1

matrix2[1][0]: 1

matrix2[1][1]: 4

Resultant Matrix after Subtraction (matrix1 - matrix2):


3 6

2 5
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
Problem Statement 03: Develop a C program to read a square matrix M x M &
then find the sum of the elements around the peripheral. Display their final sum to
the console.

Objective: To compute the sum of all elements located on the border (peripheral)
of a square matrix.

Solution:
#include <stdio.h>

int main() {

int M, sum = 0;

printf("Enter the order of the square matrix (M): ");

scanf("%d", &M);

int matrix[M][M];

printf("Enter elements of the matrix (%d x %d):\n", M, M);


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

for (int j = 0; j < M; j++) {

printf("matrix[%d][%d]: ", i, j);

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

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

for (int j = 0; j < M; j++) {


if (i == 0 || i == M - 1 || j == 0 || j == M - 1) {

sum += matrix[i][j];

printf("\nSum of peripheral (border) elements: %d\n", sum);

return 0;
}
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
Output:
Enter the order of the square matrix (M): 3

Enter elements:
123

456

789

Sum of peripheral (border) elements: 40


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 04: Develop a C program to accept the names in a 2-D array and then
search a name entered by the user. Display an appropriate message to the output screen if found.
Objective : To accept multiple names into a 2-D character array and allow the user to
search for a specific name.

Solution :

#include <stdio.h>

#include <string.h>

#define MAX_NAMES 100

#define MAX_LENGTH 50

int main() {

int n, found = 0;

char names[MAX_NAMES][MAX_LENGTH];

char searchName[MAX_LENGTH];

printf("Enter the number of names: ");

scanf("%d", &n);
getchar();

printf("Enter %d names:\n", n);

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

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

fgets(names[i], MAX_LENGTH, stdin);

names[i][strcspn(names[i], "\n")] = '\0';


}

printf("\nEnter the name to search: ");

fgets(searchName, MAX_LENGTH, stdin);


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
searchName[strcspn(searchName, "\n")] = '\0';

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

if (strcmp(names[i], searchName) == 0) {

found = 1;

break;

if (found) {

printf("Name '%s' found in the list.\n", searchName);

} else {

printf("Name '%s' NOT found in the list.\n", searchName);

return 0;
}

Output :
Enter the number of names: 3

Name 1: Alice

Name 2: Bob

Name 3: Charlie

Enter the name to search: Bob

Name 'Bob' found in the list.


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 05 : Develop a C program to read two square binary matrices of order
N x N from the user and perform the XOR on the elements of the matrices and print the resultant
matrix to the output screen.
Objective : To read two square binary matrices of order N × N from the user, perform a
bitwise XOR operation on corresponding elements of the matrices, and display the resultant
matrix.

Solution :
#include <stdio.h>

int main() {

int N;

printf("Enter the order of the square binary matrices (N): ");

scanf("%d", &N);

int matrix1[N][N], matrix2[N][N], result[N][N];

printf("Enter elements of the first binary matrix (%d x %d):\n", N, N);

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

for (int j = 0; j < N; j++) {

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

printf("Enter elements of the second binary matrix (%d x %d):\n", N, N);

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

for (int j = 0; j < N; j++) {

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

}
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
}

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

for (int j = 0; j < N; j++) {

result[i][j] = matrix1[i][j] ^ matrix2[i][j];

printf("Resultant matrix after XOR operation:\n");


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

for (int j = 0; j < N; j++) {

printf("%d ", result[i][j]);

printf("\n");

return 0;

Output :
Enter the order of the square binary matrices (N): 2

Enter elements of the first binary matrix:

10

01

Enter elements of the second binary matrix:

11
01

Resultant matrix after XOR operation:

01

00
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 06 : Design a UDF that returns the by swapping all the elements
about the principal diagonal of a matrix. Develop a C program to implement the function by
accepting the rows and columns in the main program. Display the swapped matrix in the
calling program.

Objective : To create a user-defined function that swaps all elements about the principal
diagonal of a square matrix, and then displays the modified matrix from the main (calling)
program.

Solution :
#include <stdio.h>

#define MAX 100


void swapAboutDiagonal(int matrix[MAX][MAX], int n) {

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

for (int j = i + 1; j < n; j++) {

int temp = matrix[i][j];

matrix[i][j] = matrix[j][i];

matrix[j][i] = temp;

}
}

int main() {

int matrix[MAX][MAX];

int n;

printf("Enter the order of the square matrix (n x n): ");

scanf("%d", &n);

printf("Enter the elements of the matrix:\n");


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

for (int j = 0; j < n; j++) {

printf("matrix[%d][%d]: ", i, j);

scanf("%d", &matrix[i][j]);
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
}

}
swapAboutDiagonal(matrix, n);

printf("\nMatrix after swapping elements about the principal diagonal:\n");

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

for (int j = 0; j < n; j++) {

printf("%d ", matrix[i][j]);

printf("\n");
}

return 0;

Objective:
Enter the order of the square matrix (n x n): 3

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

Matrix after swapping elements about the principal diagonal:

147
258

369
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 07: Write a C program to read a sentence from the user and replace
all the occurrences of vowels with a X character. Display the modified string to the console.

Objective : C program that reads a sentence from the user, replaces all vowels (a, e, i, o, u
in both lowercase and uppercase) with the character 'X', and displays the modified string.

Solution :
#include <stdio.h>

#include <string.h>

int main() {

char sentence[200];

printf("Enter a sentence: ");

fgets(sentence, sizeof(sentence), stdin);

for (int i = 0; sentence[i] != '\0'; i++) {

char ch = sentence[i];

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

sentence[i] = 'X';
}

printf("Modified sentence: %s", sentence);

return 0;

}
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Output :
Enter a sentence: Hello World, how are you?
Modified sentence: HXllX WXrld, hXw XrX yXX?
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 08: Develop a C program to concatenate two strings and display the
concatenated string to the output screen without using the inbuilt function.
Objective : C program that concatenates two strings without using any built-in functions like
strcat() from <string.h>. It manually appends the second string to the end of the first string and
then displays the result.
Solution :
#include <stdio.h>

int main() {

char str1[200], str2[100];


int i = 0, j = 0;

printf("Enter the first string: ");

fgets(str1, sizeof(str1), stdin);

printf("Enter the second string: ");

fgets(str2, sizeof(str2), stdin);

while (str1[i] != '\0') {


if (str1[i] == '\n') {

str1[i] = '\0';

break;

i++;

while (str2[j] != '\0') {

if (str2[j] == '\n') {

str2[j] = '\0';

}
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
str1[i] = str2[j];

i++;
j++;

str1[i] = '\0';

printf("Concatenated string: %s\n", str1);

return 0;

Output :
Enter the first string: Hello

Enter the second string: World

Concatenated string: HelloWorld


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 09 : Write a C program that accepts a main string along with a
starting position and ending position of a substring to be extracted from the user in the main
program. Print the extracted string in the calling program.

Objective: Here's a C program that:

 Accepts a main string from the user.


 Takes a starting position and ending position.
 Extracts the substring between those positions.
 Prints the extracted substring.

Solution :
#include <stdio.h>

void extractSubstring(char mainStr[], char subStr[], int start, int end) {

int j = 0;

for (int i = start; i <= end && mainStr[i] != '\0'; i++) {

subStr[j++] = mainStr[i];

subStr[j] = '\0';

int main() {

char mainStr[200], subStr[200];

int start, end;

printf("Enter the main string: ");

fgets(mainStr, sizeof(mainStr), stdin);

for (int i = 0; mainStr[i] != '\0'; i++) {

if (mainStr[i] == '\n') {

mainStr[i] = '\0';

break;
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
}

printf("Enter starting position (0-based index): ");

scanf("%d", &start);

printf("Enter ending position (0-based index): ");

scanf("%d", &end);

extractSubstring(mainStr, subStr, start, end);

printf("Extracted substring: %s\n", subStr);

return 0;

Objective:

Enter the main string: Programming

Enter starting position (0-based index): 3

Enter ending position (0-based index): 6

Extracted substring: gram


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 10: Develop a C program to compare two strings using a UDF and
return 1 if the strings are equal and 0 otherwise without using the built-in function strcmp().
Display an appropriate message to the output screen.

Objective : Here's a C program that compares two strings using a User-Defined Function
(UDF) without using the built-in strcmp() function. It returns 1 if the strings are equal, and 0
otherwise, then displays an appropriate message.

Solution :
#include <stdio.h>

int compareStrings(char str1[], char str2[]) {

int i = 0;

while (str1[i] != '\0' && str2[i] != '\0') {

if (str1[i] != str2[i]) {

return 0; // Characters differ

i++;
}

if (str1[i] == '\0' && str2[i] == '\0') {

return 1;

} else {

return 0;

}
int main() {

char str1[100], str2[100];

int result;

printf("Enter the first string: ");

fgets(str1, sizeof(str1), stdin);

printf("Enter the second string: ");

fgets(str2, sizeof(str2), stdin);


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
for (int i = 0; str1[i] != '\0'; i++) {

if (str1[i] == '\n') {
str1[i] = '\0';

break;

for (int i = 0; str2[i] != '\0'; i++) {

if (str2[i] == '\n') {

str2[i] = '\0';
break;

result = compareStrings(str1, str2);

if (result == 1) {

printf("The strings are equal.\n");

} else {
printf("The strings are NOT equal.\n");

return 0;

Output:
Enter the first string: Hello

Enter the second string: Hello

The strings are equal.

Enter the first string: Hello


Enter the second string: World

The strings are NOT equal.


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 11: Develop a C program to find the number of occurrences of each
alphabet in a string accepted from the user and display the same to the output screen. Assume
that the string contains only alphabets.

Objective: Here's a C program that counts the number of occurrences of each alphabet (A–
Z or a–z) in a string entered by the user. The program assumes that the string contains only
alphabetic characters and is case-insensitive (i.e., treats 'A' and 'a' as the same).

Solution :
#include <stdio.h>

int main() {

char str[200];

int count[26] = {0};

printf("Enter a string (alphabets only): ");

fgets(str, sizeof(str), stdin);

for (int i = 0; str[i] != '\0'; i++) {

char ch = str[i];

if (ch >= 'A' && ch <= 'Z') {

count[ch - 'A']++;

} else if (ch >= 'a' && ch <= 'z') {

count[ch - 'a']++;

}
}

printf("Occurrences of each alphabet:\n");

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

if (count[i] > 0) {
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
printf("%c = %d\n", i + 'A', count[i]);

}
}

return 0;

Output:
Enter a string (alphabets only): HelloWorld

Occurrences of each alphabet:

D=1
E=1

H=1

L=3

O=2

R=1

W=1
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 12: Write a C program to accept the string from the user and then
reverse string without using the second string.

Objective : Here's a C program to reverse a string in place, i.e., without using a second
string or any library functions like strrev().

Solution :
#include <stdio.h>

int main() {

char str[200];

int length = 0, i = 0;

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

while (str[length] != '\0') {

if (str[length] == '\n') {

str[length] = '\0';

break;
}

length++;

int start = 0;

int end = length - 1;

char temp;

while (start < end) {


temp = str[start];

str[start] = str[end];

str[end] = temp;
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

start++;
end--;

printf("Reversed string: %s\n", str);

return 0;

Output :
Enter a string: HelloWorld

Reversed string: dlroWolleH


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 13: Design a UDF in C to find the product and quotient of two
numbers passed to it from the calling program. Display the product and the quotient in the
main program. Read the two numbers in the main program & also display their product and
quotient. Implement using a pointer.

Objective : Here's a C program that uses a User Defined Function (UDF) to calculate the
product and quotient of two numbers passed from the main function. The function uses
pointers to return both results back to the main program.

Solution :
#include <stdio.h>

// User Defined Function to calculate product and quotient


void calculateProductAndQuotient(float a, float b, float *product, float *quotient) {

*product = a * b;

if (b != 0) {

*quotient = a / b;

} else {

*quotient = 0; // Handle divide-by-zero case

}
}

int main() {

float num1, num2, product, quotient;

printf("Enter the first number: ");

scanf("%f", &num1);

printf("Enter the second number: ");

scanf("%f", &num2);
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

// Call the UDF with addresses of product and quotient


calculateProductAndQuotient(num1, num2, &product, &quotient);

// Display the results in main

printf("Product: %.2f\n", product);

if (num2 != 0) {

printf("Quotient: %.2f\n", quotient);


} else {

printf("Quotient: Undefined (division by zero)\n");

return 0;

Output :
Enter the first number: 10

Enter the second number: 5


Product: 50.00

Quotient: 2.00
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 14: Write a C program to accept & store N real numbers into an array.
Find their mean and standard deviation using pointers and display the same to the output screen.
Objective : Here is a C program that:

 Accepts N real numbers (floating-point) from the user.


 Stores them in an array.
 Calculates the mean and standard deviation using pointers.
 Displays the results to the output screen

Solution :

#include <stdio.h>

#include <math.h>

// Function to calculate mean using pointers

float calculateMean(float *arr, int n) {

float sum = 0.0;

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

sum += *(arr + i);

return sum / n;

// Function to calculate standard deviation using pointers

float calculateStdDev(float *arr, int n, float mean) {

float sumSqDiff = 0.0;

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

float diff = *(arr + i) - mean;

sumSqDiff += diff * diff;


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
}

return sqrt(sumSqDiff / n);

int main() {

int n;

float numbers[100];

printf("Enter the number of real numbers (N): ");

scanf("%d", &n);

if (n <= 0 || n > 100) {

printf("Invalid number of elements.\n");

return 1;

printf("Enter %d real numbers:\n", n);

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

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

scanf("%f", &numbers[i]);

float mean = calculateMean(numbers, n);

float stdDev = calculateStdDev(numbers, n, mean);


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
printf("\nMean = %.2f\n", mean);

printf("Standard Deviation = %.2f\n", stdDev);

return 0;

Output :

Enter the number of real numbers (N): 5

Enter 5 real numbers:

1.0

2.0

3.0

4.0

5.0

Mean = 3.00

Standard Deviation = 1.41


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 15: Write a C program to accept N integers from the user into an
array. If any integer is positive then subtract 2 from it, if integer is negative add 1 to it and if
zero then do nothing. Implement a UDF using a pointer to perform these operations and
finally display the modified array to the screen in the calling program.

Objective: Here's a C program that accepts N integers from the user, modifies the integers
based on their value (positive, negative, or zero), and displays the modified array. The core
logic is implemented in a User Defined Function (UDF) that uses pointers.

Solution:
#include <stdio.h>

// UDF to modify the array using pointer

void modifyArray(int *arr, int size) {

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

if (*(arr + i) > 0) {

*(arr + i) -= 2;

} else if (*(arr + i) < 0) {

*(arr + i) += 1;
}

// If zero, do nothing

int main() {

int N;

// Get the number of elements

printf("Enter the number of integers (N): ");

scanf("%d", &N);
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
int arr[N]; // Declare array of size N

// Input array elements

printf("Enter %d integers:\n", N);

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

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

// Call UDF to modify array


modifyArray(arr, N);

// Display modified array

printf("Modified array:\n");

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

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

return 0;

Output :
Enter the number of integers (N): 6

Enter 6 integers:

5 -3 0 8 -1 2

Modified array:

3 -2 0 6 0 0
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 16: Design a UDF using pointer that returns 1 if the string is a
palindrome otherwise zero. Accept a string from user in the calling program. Display an
appropriate message in the calling program. Implement a C program for the same. Ignore the
case.

Objective: Here's a C program that checks if a string is a palindrome using a User


Defined Function (UDF) that works with pointers. The comparison is case-
insensitive, and the function returns 1 if the string is a palindrome, otherwise 0.

Solution:
#include <stdio.h>

#include <string.h>
#include <ctype.h>

// UDF to check if the string is a palindrome using pointers

int isPalindrome(char *str) {

char *start = str;

char *end = str + strlen(str) - 1;

while (start < end) {

// Convert both characters to lowercase for case-insensitive comparison

if (tolower(*start) != tolower(*end)) {

return 0; // Not a palindrome

start++;

end--;

return 1; // Palindrome

}
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
int main() {

char str[100];

// Input string from user

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

// Remove newline character if any

str[strcspn(str, "\n")] = '\0';

// Call the UDF

if (isPalindrome(str)) {

printf("The string \"%s\" is a palindrome.\n", str);

} else {

printf("The string \"%s\" is NOT a palindrome.\n", str);

return 0;

Output:
Enter a string: Madam

The string "Madam" is a palindrome.

Enter a string: Hello

The string "Hello" is NOT a palindrome.


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 17: Design a UDF that returns the count of special characters
present in a sentence passed to it using pointer to the calling program. Implement a C
program to read a string in the main program and display all the count of the special
characters returned by the function to the console.

Objective: Here's a C program that counts the number of special characters in a string
using a User Defined Function (UDF). The function uses pointers and returns the count to the
calling program.

Solution:
#include <stdio.h>

#include <ctype.h>

// UDF to count special characters using pointer

int countSpecialChars(char *str) {

int count = 0;

while (*str != '\0') {

if (!isalnum(*str) && !isspace(*str)) {

count++;
}

str++; // Move pointer to next character

return count;

int main() {

char sentence[200];

// Read input string

printf("Enter a sentence: ");

fgets(sentence, sizeof(sentence), stdin);


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

// Remove newline if present


sentence[strcspn(sentence, "\n")] = '\0';

// Call UDF to count special characters

int specialCount = countSpecialChars(sentence);

// Display result

printf("Number of special characters: %d\n", specialCount);

return 0;

Output :
Enter a sentence: Hello, World! @2025

Number of special characters: 3

Enter a sentence: This is a test.

Number of special characters: 1


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 18: Design a UDF that takes a single string and a character present
in the string as its argument, then it returns an index to the first occurrence of the character to
the string. Write a C program to read a sentence in the main program and display the string
from the index returned by the UDF using a pointer to the console.

Objective: Here's a C program with a User Defined Function (UDF) that:


 Takes a string and a character as arguments.

 Returns the index of the first occurrence of that character in the string.

 The main program then displays the substring starting from that index using pointer
arithmetic.

Solution:
#include <stdio.h>

#include <string.h>

int findCharIndex(char *str, char ch) {

int index = 0;

while (*str != '\0') {


if (*str == ch) {

return index;

str++;

index++;

return -1; // Character not found


}

int main() {

char sentence[200], ch;

printf("Enter a sentence: ");

fgets(sentence, sizeof(sentence), stdin);

sentence[strcspn(sentence, "\n")] = '\0'; // Remove newline if any

printf("Enter a character to search for: ");


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
scanf(" %c", &ch);

int index = findCharIndex(sentence, ch);


if (index == -1) {

printf("Character '%c' not found in the sentence.\n", ch);

} else {

printf("Character '%c' found at index %d.\n", ch, index);

// Display substring from that index using pointer

printf("Substring from that index: %s\n", sentence + index);

}
return 0;

Output:
Enter a sentence: Programming in C is powerful!

Enter a character to search for: i

Character 'i' found at index 5.

Substring from that index: ing in C is powerful!

Enter a sentence: Hello World

Enter a character to search for: z


Character 'z' not found in the sentence.
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 19: Write a C program to dynamically allocate memory for an


integer array of size N, allow the user to input N integers, resize the array to N+M using
realloc to add M more integers, compute the sum of all N+M integers, and properly release
the allocated memory.

Objective : Here's a complete C program that:


 Dynamically allocates memory for N integers using malloc.

 Accepts N integers from the user.

 Uses realloc to resize the array to N + M to add more integers.

 Computes the sum of all N + M integers.

 Properly frees the allocated memory.

Solution:
#include <stdio.h>
#include <stdlib.h>

int main() {

int *arr = NULL;

int N, M, i, sum = 0;

// Input initial size N

printf("Enter the number of integers (N): ");


scanf("%d", &N);

// Allocate memory for N integers

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

if (arr == NULL) {

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

return 1;
}
Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
// Input N integers

printf("Enter %d integers:\n", N);


for (i = 0; i < N; i++) {

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

// Input additional size M

printf("Enter the number of additional integers to add (M): ");

scanf("%d", &M);

// Reallocate memory to hold N + M integers

arr = (int *)realloc(arr, (N + M) * sizeof(int));

if (arr == NULL) {

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

return 1;

// Input M more integers

printf("Enter %d more integers:\n", M);

for (i = N; i < N + M; i++) {

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

// Compute the sum of all N + M integers


for (i = 0; i < N + M; i++) {

sum += arr[i];

// Display the sum


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
printf("The sum of all %d integers is: %d\n", N + M, sum);

// Free the allocated memory

free(arr);

return 0;

Output:
Enter the number of integers (N): 3

Enter 3 integers:
10 20 30

Enter the number of additional integers to add (M): 2

Enter 2 more integers:

5 15

The sum of all 5 integers is: 80


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem

Problem Statement 20: Write a C program to dynamically allocate memory for two
user-input strings, concatenate them into a new dynamically allocated string, print the result
and free all allocated memory.

Objective: Here's a C program that:


 Dynamically allocates memory for two user-input strings.

 Concatenates them into a new dynamically allocated string.

 Prints the concatenated result.

 Frees all allocated memory.

Solution:
#include <stdio.h>
#include <stdlib.h>

#include <string.h>

int main() {

char *str1, *str2, *concat;

int len1, len2;

// Allocate memory for first string

printf("Enter the first string: ");

str1 = (char *)malloc(100 * sizeof(char)); // Temporary large buffer

if (str1 == NULL) {

printf("Memory allocation failed for first string.\n");

return 1;

fgets(str1, 100, stdin);


str1[strcspn(str1, "\n")] = '\0'; // Remove newline

// Allocate memory for second string


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
printf("Enter the second string: ");

str2 = (char *)malloc(100 * sizeof(char));


if (str2 == NULL) {

printf("Memory allocation failed for second string.\n");

free(str1);

return 1;

fgets(str2, 100, stdin);

str2[strcspn(str2, "\n")] = '\0'; // Remove newline

// Calculate lengths and allocate memory for concatenated string

len1 = strlen(str1);

len2 = strlen(str2);

concat = (char *)malloc((len1 + len2 + 1) * sizeof(char)); // +1 for '\0'

if (concat == NULL) {

printf("Memory allocation failed for concatenated string.\n");


free(str1);

free(str2);

return 1;

// Concatenate strings

strcpy(concat, str1);

strcat(concat, str2);

// Print result

printf("Concatenated string: %s\n", concat);

// Free allocated memory


Name : Aryan Chauhan Class Roll no : 12
Section: D1 Course : BCA 2nd Sem
free(str1);

free(str2);
free(concat);

return 0;

Output:
Enter the first string: Hello

Enter the second string: World


Concatenated string: HelloWorld

You might also like