module 3 important questions
module 3 important questions
⭐⭐1. Explain the String handling functions in C with examples.(3 marks and above)
Ans: 1. `strlen`:
Definition: `strlen` is used to calculate the length of a given string, excluding the null terminator '\0'.
Example: `int length = strlen("Hello"); // Returns 5`
2. `strcpy`:
Definition: `strcpy` copies the contents of one string to another, including the null terminator '\0'.
Example: `char destination[20]; strcpy(destination, "Hello"); // destination contains "Hello"`
3. `strcat`:
Definition: `strcat` appends the characters of the second string to the end of the first string and adds a null
terminator.
Example: `char str1[30] = "Hello"; strcat(str1, ", world!"); // str1 contains "Hello, world!"`
4. `strcmp`:
Definition: `strcmp` compares two strings lexicographically and returns an integer indicating their
relationship.
Example: `int result = strcmp("apple", "banana"); // Returns a value less than 0`
5. `puts`:
Definition: `puts` writes a string to the standard output followed by a newline character.
Example: `puts("Hello, world!"); // Outputs "Hello, world!"`
6. `gets` (Deprecated and Unsafe):
Definition: `gets` reads a line from the standard input and stores it in the provided buffer.
Example: `char name[30]; gets(name); // Reads user input into 'name' (unsafe, avoid using)`
⭐⭐9. Without using any built in string processing function like strlen, strcat etc., write a program to
concatenate two strings. (7 marks)
Ans: #include <stdio.h>
int main() {
// Define two arrays to store the strings
char str1[100], str2[100];
// Input the first string
printf("Enter the first string: ");
scanf("%s", str1);
// Input the second string
printf("Enter the second string: ");
scanf("%s", str2);
// Find the length of the first string
int len1 = 0;
while (str1[len1] != '\0')
len1++;
// Concatenate the second string to the end of the first string
int i, j = 0;
for (i = len1; str2[j] != '\0'; i++, j++)
str1[i] = str2[j];
str1[i] = '\0';
// Print the concatenated string
printf("Concatenated string: %s\n", str1);
return 0;
}
This program first reads two strings from the user, then iterates through the first string to find its length.
After that, it uses a loop to concatenate the characters of the second string onto the end of the first string.
Finally, it prints the concatenated string.
10. Write a C program to check whether a given matrix is a diagonal matrix. (7 marks)
Ans: #include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
// Input the matrix elements
printf("Enter the matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Check if the matrix is diagonal
int isDiagonal = 1; // Assume the matrix is diagonal
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// If an off-diagonal element is non-zero, it's not diagonal
if (i != j && matrix[i][j] != 0) {
isDiagonal = 0; // Mark the matrix as not diagonal
break; // Exit the inner loop
}
}
if (!isDiagonal) {
break; // Exit the outer loop
}
}
// Print the result
if (isDiagonal) {
printf("The matrix is a diagonal matrix.\n");
} else {
printf("The matrix is not a diagonal matrix.\n");
}
return 0;
}
This program first takes the dimensions of the matrix as input, then reads the matrix elements from the
user. It checks whether the matrix is diagonal by iterating through its elements and checking if any off-
diagonal element is non-zero. If it finds such an element, it concludes that the matrix is not diagonal.
Otherwise, it considers the matrix as diagonal and prints the appropriate result.
int main() {
// Define variables for matrix dimensions
int rows, cols;
// Input number of rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare the matrix
int matrix[rows][cols];
// Input matrix elements
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Display original matrix
printf("Original matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Transpose the matrix
int transpose[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
transpose[i][j] = matrix[j][i];
}
}
// Display transposed matrix
printf("Transposed matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
This program finds the transpose of a matrix. This program takes user input for matrix dimensions and
elements, displays the original matrix, calculates the transpose, and then displays the transposed matrix.
⭐12. Write a C program to reverse a string without using string handling functions. (7 marks)
⭐13. Write a C program to print number of vowels and consonants in a string. (7 marks)
Ans: #include <stdio.h>
#include <ctype.h> // Include the header for 'toupper' function
int main() {
// Declare a character array to store the input string
char input[100];
// Input the string from the user
printf("Enter a string: ");
scanf("%[^\n]", input);
// Initialize variables to count vowels and consonants
int vowels = 0, consonants = 0;
// Iterate through each character in the string
for (int i = 0; input[i] != '\0'; i++) {
// Convert the character to uppercase using 'toupper' function
char ch = toupper(input[i]);
// Check if the character is an alphabet letter
if ((ch >= 'A' && ch <= 'Z')) {
// Check if the character is a vowel
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
vowels++;
} else {
consonants++;
}
}
}
// Display the counts of vowels and consonants
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
return 0;
}
The program takes user input for a string, iterates through each character, converts it to uppercase using
`toupper`, checks if it's an alphabet letter, and then counts vowels and consonants accordingly. Finally, it
displays the counts of vowels and consonants in the string.
14. Write a C program to find the sum and average of elements of a matrix. (7 marks)
Ans: #include <stdio.h>
int main() {
// Define variables for matrix dimensions
int rows, cols;
// Input number of rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare the matrix
int matrix[rows][cols];
// Input matrix elements
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Calculate the sum of elements
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += matrix[i][j];
}
}
// Calculate the average of elements
float average = (float)sum / (rows * cols);
// Display the sum and average
printf("Sum of elements: %d\n", sum);
printf("Average of elements: %f\n", average);
return 0;
}
This C program calculates the sum and average of elements in a matrix. The program takes user input for
matrix dimensions and elements, calculates the sum of elements, calculates the average by dividing the
sum by the total number of elements, and then displays the sum and average.
⭐15. Write a C program that reads a string from keyboard and determines whether the string is
palindrome or not. (7 marks)
Ans: #include <stdio.h>
int main() {
// Declare a character array to store the input string
char input[100];
// Input the string from the user
printf("Enter a string: ");
scanf("%s", input);
// Calculate the length of the string
int length = 0;
while (input[length] != '\0') {
length++;
}
// Initialize a flag for palindrome check
int isPalindrome = 1; // 1 indicates palindrome, 0 indicates not palindrome
// Check if the string is a palindrome
for (int i = 0, j = length - 1; i < j; i++, j--) {
if (input[i] != input[j]) {
isPalindrome = 0;
break;
}
}
// Display whether the string is palindrome or not
if (isPalindrome) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
This C program checks whether a given string is a palindrome or not. The program takes user input for a
string, calculates the length of the string, initializes a flag for palindrome check (1 for palindrome, 0 for not
palindrome), iterates through the string to compare characters from both ends using a `for` loop, and finally
displays whether the string is a palindrome or not.