0% found this document useful (0 votes)
2 views30 pages

C Programming Array and String Operations

Uploaded by

waghviraj1409
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)
2 views30 pages

C Programming Array and String Operations

Uploaded by

waghviraj1409
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

Problem sheet-2

Q1
#include <stdio.h>

void sort1D(int *arr, int n)


{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (*(arr + j) > *(arr + j + 1))
{
temp = ×(arr + j);
×(arr + j) = *(arr + j + 1);
×(arr + j + 1) = temp;
}
}
}
}

void print1D(int *arr, int n)


{
for (int i = 0; i < n; i++)
{
printf("%d ", *(arr + i));
}
printf("\n");
}

int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++)
{
scanf("%d", (arr + i));
}

sort1D(arr, n);
printf("Sorted 1D array: ");
print1D(arr, n);

return 0;
}

q2
#include <stdio.h>

void sort2D(int *arr, int rows, int cols)


{
int size = rows * cols;
int i, j, temp;
for (i = 0; i < size - 1; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (*(arr + j) > *(arr + j + 1))
{
temp = ×(arr + j);
×(arr + j) = *(arr + j + 1);
×(arr + j + 1) = temp;
}
}
}
}

void print2D(int *arr, int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", *(arr + i * cols + j));
}
printf("\n");
}
}

int main()
{
int rows, cols;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];

printf("Enter %d elements: ", rows * cols);


for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", (*(arr + i) + j));
}
}

sort2D((int *)arr, rows, cols);


printf("Sorted 2D array:\n");
print2D((int *)arr, rows, cols);

return 0;
}

q3
#include <stdio.h>

int sum1D(int *arr, int n)


{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + ×(arr + i);
}
return sum;
}

int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++)
{
scanf("%d", (arr + i));
}

printf("Sum of 1D array elements: %d\n", sum1D(arr, n));

return 0;
}
q4
#include <stdio.h>

int sum2D(int *arr, int rows, int cols)


{
int sum = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sum = sum + ×(arr + i × cols + j);
}
}
return sum;
}

int main()
{
int rows, cols;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];

printf("Enter %d elements: ", rows * cols);


for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", (*(arr + i) + j));
}
}

printf("Sum of 2D array elements: %d\n", sum2D((int *)arr, rows, cols));

return 0;
}
q5
#include <stdio.h>

int compareStrings(char *str1, char *str2)


{
while (*str1 && *str2)
{
if (×str1 != *str2)
{
return (*str1 - *str2);
}
str1++;
str2++;
}
return (*str1 - *str2);
}

int main()
{
char str1[100], str2[100];

printf("Enter first string: ");


scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);

int result = compareStrings(str1, str2);

if (result == 0)
printf("Strings are equal.\n");
else if (result > 0)
printf("First string is greater.\n");
else
printf("Second string is greater.\n");

return 0;
}
q6
#include <stdio.h>

void concatenateStrings(char *str1, char *str2)


{
while (*str1)
{
str1++;
}
while (*str2)
{
×str1 = *str2;
str1++;
str2++;
}
×str1 = '\0';
}

int main()
{
char str1[200], str2[100];

printf("Enter first string: ");


scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);

concatenateStrings(str1, str2);

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

return 0;
}

q7
#include <stdio.h>
int isPalindrome(char *str)
{
char *start = str;
char *end = str;

while (*end)
{
end++;
}
end--;

while (start < end)


{
if (*start != *end)
{
return 0;
}
start++;
end--;
}
return 1;
}

int main()
{
char str[100];

printf("Enter a string: ");


scanf("%s", str);

if (isPalindrome(str))
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");

return 0;
}

q8
#include <stdio.h>
void copyString(char *source, char *destination)
{
while (*source)
{
*destination = *source;
source++;
destination++;
}
*destination = '\0';
}

int main()
{
char source[100], destination[100];

printf("Enter the source string: ");


scanf("%s", source);

copyString(source, destination);

printf("Copied string: %s\n", destination);

return 0;
}
q9
#include <stdio.h>

int isPrime(int num)


{
if (num < 2)
return 0;
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
return 0;
}
return 1;
}

int countPrimes(int *arr, int n)


{
int count = 0;
for (int i = 0; i < n; i++)
{
if (isPrime(*(arr + i)))
{
count++;
}
}
return count;
}

int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++)
{
scanf("%d", (arr + i));
}

printf("Total prime numbers: %d\n", countPrimes(arr, n));

return 0;
}

q10
#include <stdio.h>

int power(int base, int exp)


{
int result = 1;
for (int i = 0; i < exp; i++)
{
result ×= base;
}
return result;
}

int isArmstrong(int *num)


{
int n = *num, sum = 0, digits = 0, temp = *num;

// Count number of digits


while (temp)
{
digits++;
temp /= 10;
}

temp = *num;
while (temp)
{
int digit = temp % 10;
sum += power(digit, digits);
temp /= 10;
}

return (sum == *num);


}

int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (isArmstrong(&num))
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

return 0;
}

q11
#include <stdio.h>
void reverseArray(int *arr, int n)
{
int *start = arr;
int *end = arr + n - 1;
while (start < end)
{
int temp = *start;
*start = *end;
*end = temp;

start++;
end--;
}
}

int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++)
{
scanf("%d", (arr + i));
}

reverseArray(arr, n);

printf("Reversed array: ");


for (int i = 0; i < n; i++)
{
printf("%d ", *(arr + i));
}
printf("\n");

return 0;
}

q12
#include <stdio.h>

void reverseString(char *str)


{
char *start = str;
char *end = str;

while (*end)
{
end++;
}
end--;
while (start < end)
{

char temp = *start;


*start = *end;
*end = temp;

start++;
end--;
}
}

int main()
{
char str[100];

printf("Enter a string: ");


scanf("%s", str);

reverseString(str);

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

return 0;
}

q13
#include <stdio.h>
void insertElement(int *arr, int *size, int element, int position)
{
if (position < 0 || position > *size)
{
printf("Invalid position!\n");
return;
}
for (int i = *size; i > position; i--)
{
arr[i] = arr[i - 1];
}
arr[position] = element;
(*size)++;
}

void updateElement(int *arr, int size, int element, int position)


{
if (position < 0 || position >= size)
{
printf("Invalid position!\n");
return;
}
arr[position] = element;
}

void deleteElement(int *arr, int *size, int position)


{
if (position < 0 || position >= *size)
{
printf("Invalid position!\n");
return;
}
for (int i = position; i < *size - 1; i++)
{
arr[i] = arr[i + 1];
}
(*size)--;
}

void printArray(int *arr, int size)


{
printf("Array: ");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int main()
{
int arr[100], size, element, position;

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


scanf("%d", &size);

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

printArray(arr, size);

printf("Enter element to insert and position: ");


scanf("%d %d", &element, &position);
insertElement(arr, &size, element, position);
printArray(arr, size);

printf("Enter element to update and position: ");


scanf("%d %d", &element, &position);
updateElement(arr, size, element, position);
printArray(arr, size);

printf("Enter position to delete: ");


scanf("%d", &position);
deleteElement(arr, &size, position);
printArray(arr, size);

return 0;
}
q14
#include <stdio.h>
#include <stdlib.h>

void inputMatrix(int *mat, int rows, int cols)


{
printf("Enter the matrix elements:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", (mat + i * cols + j));
}
}
}

void displayMatrix(int *mat, int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d\t", *(mat + i * cols + j));
}
printf("\n");
}
}

void addMatrices(int *mat1, int *mat2, int *result, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
×(result + i × cols + j) = *(mat1 + i * cols + j) + *(mat2 + i * cols + j);
}
}
}

void subtractMatrices(int *mat1, int *mat2, int *result, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
×(result + i × cols + j) = *(mat1 + i * cols + j) - *(mat2 + i * cols + j);
}
}
}

void multiplyMatrices(int *mat1, int *mat2, int *result, int rows1, int cols1, int cols2)
{
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols2; j++)
{
×(result + i × cols2 + j) = 0;
for (int k = 0; k < cols1; k++)
{
×(result + i × cols2 + j) += *(mat1 + i * cols1 + k) * *(mat2 + k * cols2 + j);
}
}
}
}

void transposeMatrix(int *mat, int *result, int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
×(result + j × rows + i) = *(mat + i * cols + j);
}
}
}

int main()
{
int rows1, cols1, rows2, cols2;

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


scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for matrix 2: ");
scanf("%d %d", &rows2, &cols2);

if (rows1 != rows2 || cols1 != cols2)


{
printf("Matrix addition and subtraction require matrices of the same dimensions.\n");
}

if (cols1 != rows2)
{
printf("Matrix multiplication requires the number of columns in matrix 1 to be equal to
the number of rows in matrix 2.\n");
}

int ×mat1 = (int *)malloc(rows1 * cols1 * sizeof(int));


int ×mat2 = (int *)malloc(rows2 * cols2 * sizeof(int));
int ×result = (int *)malloc(rows1 * cols2 * sizeof(int));

printf("For matrix 1:\n");


inputMatrix(mat1, rows1, cols1);
printf("For matrix 2:\n");
inputMatrix(mat2, rows2, cols2);

printf("Matrix 1:\n");
displayMatrix(mat1, rows1, cols1);
printf("Matrix 2:\n");
displayMatrix(mat2, rows2, cols2);

if (rows1 == rows2 && cols1 == cols2)


{
addMatrices(mat1, mat2, result, rows1, cols1);
printf("Matrix addition result:\n");
displayMatrix(result, rows1, cols1);
}

if (rows1 == rows2 && cols1 == cols2)


{
subtractMatrices(mat1, mat2, result, rows1, cols1);
printf("Matrix subtraction result:\n");
displayMatrix(result, rows1, cols1);
}

if (cols1 == rows2)
{
multiplyMatrices(mat1, mat2, result, rows1, cols1, cols2);
printf("Matrix multiplication result:\n");
displayMatrix(result, rows1, cols2);
}

int *transposeResult = (int *)malloc(cols1 * rows1 * sizeof(int));


transposeMatrix(mat1, transposeResult, rows1, cols1);
printf("Transpose of matrix 1:\n");
displayMatrix(transposeResult, cols1, rows1);

free(mat1);
free(mat2);
free(result);
free(transposeResult);

return 0;
}

q15
#include <stdio.h>
#include <stdlib.h>

void inputMatrix(int *mat, int rows, int cols)


{
printf("Enter the matrix elements:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", (mat + i * cols + j));
}
}
}

void displayMatrix(int *mat, int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d\t", *(mat + i * cols + j));
}
printf("\n");
}
}

void reverseMatrix(int *mat, int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols / 2; j++)
{
int temp = ×(mat + i × cols + j);
×(mat + i × cols + j) = *(mat + i * cols + (cols - j - 1));
×(mat + i × cols + (cols - j - 1)) = temp;
}
}
}

int main()
{
int rows, cols;
printf("Enter the number of rows and columns for the matrix: ");
scanf("%d %d", &rows, &cols);

int *mat = (int *)malloc(rows * cols * sizeof(int));

inputMatrix(mat, rows, cols);


printf("Original Matrix:\n");
displayMatrix(mat, rows, cols);

reverseMatrix(mat, rows, cols);


printf("Reversed Matrix:\n");
displayMatrix(mat, rows, cols);

free(mat);
return 0;
}
q16
#include <stdio.h>

int countVowels(char *str)


{
int count = 0;
while (*str)
{
char ch = *str;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
count++;
}
str++;
}
return count;
}

int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s", str);

int vowelCount = countVowels(str);


printf("Number of vowels in the string: %d\n", vowelCount);

return 0;
}

q17
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
if (argc != 2)
{
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}

FILE *file = fopen(argv[1], "r");


if (file == NULL)
{
printf("Error: Cannot open file %s\n", argv[1]);
return 1;
}

char ch;
while ((ch = fgetc(file)) != EOF)
{
putchar(ch);
}

fclose(file);
return 0;
}

q18
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
if (argc != 3)
{
printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
return 1;
}

FILE *source = fopen(argv[1], "r");


if (source == NULL)
{
printf("Error: Cannot open source file %s\n", argv[1]);
return 1;
}

FILE *destination = fopen(argv[2], "w");


if (destination == NULL)
{
printf("Error: Cannot create destination file %s\n", argv[2]);
fclose(source);
return 1;
}

char ch;
while ((ch = fgetc(source)) != EOF)
{
fputc(ch, destination);
}

fclose(source);
fclose(destination);

printf("File copied successfully from %s to %s\n", argv[1], argv[2]);


return 0;
}

q19
#include <stdio.h>
#include <stdlib.h>

long long factorial(int n)


{
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}

int main(int argc, char *argv[])


{
if (argc != 2)
{
printf("Usage: %s <number>\n", argv[0]);
return 1;
}

int num = atoi(argv[1]);


if (num < 0)
{
printf("Factorial is not defined for negative numbers.\n");
return 1;
}

printf("Factorial of %d is %lld\n", num, factorial(num));


return 0;
}
q20
#include <stdio.h>
#include <stdlib.h>

int isPrime(int n)
{
if (n < 2)
return 0;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return 0;
}
return 1;
}

int main(int argc, char *argv[])


{
if (argc != 2)
{
printf("Usage: %s <number>\n", argv[0]);
return 1;
}

int num = atoi(argv[1]);

if (num <= 0)
{
printf("Enter a positive integer.\n");
return 1;
}

if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}
q21
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
if (argc < 2)
{
printf("Usage: %s <num1> <num2> ... <numN>\n", argv[0]);
return 1;
}

int sum = 0;
for (int i = 1; i < argc; i++)
{
sum += atoi(argv[i]);
}

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


return 0;
}
q22
#include <stdio.h>
#include <stdlib.h>

void bubbleSort(int arr[], int n)


{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main(int argc, char *argv[])


{
if (argc != 11)
{
printf("Usage: %s <10 numbers>\n", argv[0]);
return 1;
}

int arr[10];
for (int i = 1; i <= 10; i++)
{
arr[i - 1] = atoi(argv[i]);
}

bubbleSort(arr, 10);

printf("Sorted numbers: ");


for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
q23
#include <stdio.h>
#include <ctype.h>

int main()
{
FILE *file;
char ch;
int vowels = 0, consonants = 0;

file = fopen("[Link]", "r");


if (file == NULL)
{
printf("Error: Cannot open file [Link]\n");
return 1;
}

while ((ch = fgetc(file)) != EOF)


{
ch = tolower(ch);
if ((ch >= 'a' && ch <= 'z'))
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
}

fclose(file);

printf("Vowels: %d\n", vowels);


printf("Consonants: %d\n", consonants);

return 0;
}
q24
#include <stdio.h>

void displayFile(const char *filename)


{
FILE ×file = fopen(filename, "r");
int num;

if (file == NULL)
{
printf("Error: Cannot open file %s\n", filename);
return;
}

printf("Contents of %s:\n", filename);


while (fscanf(file, "%d", &num) != EOF)
{
printf("%d ", num);
}
printf("\n");

fclose(file);
}

int main()
{
FILE *master, *odd, *even;
int num;

master = fopen("[Link]", "r");


if (master == NULL)
{
printf("Error: Cannot open [Link]\n");
return 1;
}

odd = fopen("[Link]", "w");


even = fopen("[Link]", "w");

while (fscanf(master, "%d", &num) != EOF)


{
if (num % 2 == 0)
fprintf(even, "%d\n", num);
else
fprintf(odd, "%d\n", num);
}

fclose(master);
fclose(odd);
fclose(even);

displayFile("[Link]");
displayFile("[Link]");

return 0;
}
q25
#include <stdio.h>
typedef struct
{
int rollno;
char name[50];
float percentage;
} Student;

int countRecords(const char *filename)


{
FILE ×file = fopen(filename, "rb");
if (file == NULL)
{
printf("Error: Cannot open file %s\n", filename);
return -1;
}

Student s;
int count = 0;
while (fread(&s, sizeof(Student), 1, file) == 1)
{
count++;
}

fclose(file);
return count;
}

int main()
{
int totalRecords = countRecords("[Link]");
if (totalRecords != -1)
{
printf("Total number of records: %d\n", totalRecords);
}
return 0;
}
q26
#include <stdio.h>

void readFile(const char *filename)


{
FILE ×file = fopen(filename, "r");
char ch;

if (file == NULL)
{
printf("Error: Cannot open file %s\n", filename);
return;
}

while ((ch = fgetc(file)) != EOF)


{
putchar(ch);
}
printf("\n");
fclose(file);
}

void consolidateFiles(const char *outputFile, const char *file1, const char *file2, const char
*file3)
{
FILE ×out = fopen(outputFile, "w");
FILE *in;
char ch;

if (out == NULL)
{
printf("Error: Cannot create file %s\n", outputFile);
return;
}

const char *files[] = {file1, file2, file3};


for (int i = 0; i < 3; i++)
{
in = fopen(files[i], "r");
if (in == NULL)
{
printf("Error: Cannot open file %s\n", files[i]);
continue;
}
while ((ch = fgetc(in)) != EOF)
{
fputc(ch, out);
}
fputs("\n", out);
fclose(in);
}

fclose(out);
printf("All details consolidated into %s\n", outputFile);
}

int main()
{
consolidateFiles("[Link]", "student_info.txt", "University_info.txt", "Bank_info.txt");

printf("\nDisplaying consolidated details from [Link]:\n");


readFile("[Link]");

return 0;
}

You might also like