Skip to content

Commit 5b26eb0

Browse files
committed
added
0 parents  commit 5b26eb0

File tree

12 files changed

+571
-0
lines changed

12 files changed

+571
-0
lines changed

a.out

15.7 KB
Binary file not shown.

cntwords.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
#include <ctype.h>
3+
void countAlphabetsAndWords(char str[], int*alphabetCount, int *wordCount) {
4+
int inWord = 0;
5+
*alphabetCount = 0;
6+
*wordCount = 0;
7+
for (int i = 0; str[i] != '\0'; i++) {
8+
if (isalpha(str[i])) {
9+
(*alphabetCount)++;
10+
}
11+
if (isspace(str[i])) {
12+
if (inWord) {
13+
inWord = 0;
14+
(*wordCount)++;
15+
}
16+
} else {
17+
inWord = 1;
18+
}
19+
}
20+
21+
if (inWord) {
22+
(*wordCount)++;
23+
}
24+
}
25+
int main() {
26+
char str[1000];
27+
int alphabetCount, wordCount;
28+
29+
printf("Enter a string: ");
30+
fgets(str, sizeof(str), stdin);
31+
32+
countAlphabetsAndWords(str, &alphabetCount, &wordCount);
33+
34+
printf("Number of alphabets: %d\n", alphabetCount);
35+
printf("Number of words: %d\n", wordCount);
36+
37+
return 0;
38+
39+
}

concat.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
void concatenateStrings(char str1[], char str2[], char result[]) {
4+
strcpy(result, str1);
5+
strcat(result, str2);
6+
}
7+
int main() {
8+
char str1[100], str2[100], result[200];
9+
printf("Enter the first string: ");
10+
fgets(str1, sizeof(str1), stdin);
11+
printf("Enter the second string: ");
12+
fgets(str2, sizeof(str2), stdin);
13+
14+
concatenateStrings(str1, str2, result);
15+
printf("Concatenated string: %s", result);
16+
return 0;
17+
}

ptr/matrmult.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#define MAX_SIZE 10 // max size for matrix dimensions
3+
void inputMatrix(int (*matrix)[MAX_SIZE], int rows, int
4+
cols) {
5+
printf("enter elements for the %d by %d matrix: \n",
6+
rows, cols);
7+
for (int i = 0; i < rows; i++) {
8+
for (int j = 0; j < cols; j++) {
9+
printf("Element [%d][%d]: ", i, j);
10+
scanf("%d", *(matrix + i) + j);
11+
}
12+
}
13+
}
14+
void multiplyMatrices(int (*mat1)[MAX_SIZE], int
15+
(*mat2)[MAX_SIZE], int(*result)[MAX_SIZE], int rows1, int
16+
cols1, int cols2) {
17+
//initialize the matrix to 0 using pointer arithmetic
18+
for (int i = 0; i < rows1; i++) {
19+
for (int j = 0; j < cols2; j++) {
20+
*(result[i] + j) = 0; //Correct pointer access initialize to 0
21+
for (int k = 0; k < cols2; k++) {
22+
*(result[i] + j) += *(mat1[i] +k)*
23+
*(mat2[k] + j); //matrix multiplication
24+
}
25+
}
26+
}
27+
}
28+
void printMatrix(int (*matrix)[MAX_SIZE], int rows, int
29+
cols) {
30+
for (int i = 0; i < rows; i++) {
31+
for (int j = 0; j < cols; j++) {
32+
printf("%d ", *(*(matrix + i) + j));
33+
}
34+
printf("\n");
35+
}
36+
}
37+
int main() {int rows1, cols1, rows2, cols2;
38+
printf("Enter rows and columns for the first matrix:");
39+
scanf("%d%d", &rows1, &cols1);
40+
printf("Enter rows and columns for the second matrix:");
41+
scanf("%d%d", &rows2, &cols2);
42+
int mat1[rows1][MAX_SIZE], mat2[rows2][MAX_SIZE],
43+
result[rows1][cols2];
44+
inputMatrix(mat1, rows1, cols1);
45+
inputMatrix(mat2, rows2, cols2);
46+
multiplyMatrices(mat1, mat2, result, rows1, cols1,
47+
cols2);
48+
printf("Result Matrix: \n");
49+
printMatrix(result, rows1, cols2);
50+
return 0;
51+
}

ptr/revarr.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
void swap(int* a, int* b)
3+
{
4+
int temp = *a;
5+
*a = *b;
6+
*b = temp;
7+
}
8+
void reverse(int array[], int array_size)
9+
{
10+
int *pointer1 = array,
11+
*pointer2 = array + array_size - 1;
12+
while (pointer1 < pointer2) {
13+
swap(pointer1, pointer2);
14+
pointer1++;
15+
pointer2--;}
16+
}
17+
void print(int* array, int array_size)
18+
{
19+
// Length pointing at end of the array
20+
int *length = array + array_size,
21+
*position = array;
22+
printf("Array = ");
23+
for (position = array; position < length;
24+
position++)
25+
printf("%d ", *position);
26+
}
27+
int main()
28+
{
29+
// Array to hold the values
30+
int array[] = { 2, 4, -6, 5, 8, -1 };
31+
printf("Original ");
32+
print(array, 6);
33+
printf("Reverse ");
34+
reverse(array, 6);
35+
print(array, 6);
36+
return 0;
37+
}

ptr/swaparr.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdbool.h>
4+
5+
void print(int *p, int n) {
6+
for (int i=0; i<n; i++) {
7+
printf("%d\t", *(p+i));
8+
}
9+
printf("\n");
10+
}
11+
12+
void swap(int *a, int *b) {
13+
int temp;
14+
temp = *a; // temp holds deferenced value
15+
*a = *b;
16+
*b = temp;
17+
}
18+
int main() {
19+
int arr[] = {3, 4, 7, 11};
20+
int n = sizeof(arr)/sizeof(arr[0]);
21+
int *ptr = arr;
22+
print(arr, n);
23+
int smallest = 0;
24+
int largest = 0;
25+
for (int i=0; i<n; i++) {
26+
if (*(ptr + i) < ptr[smallest]) {
27+
smallest = i;
28+
}if (*(ptr + i) > ptr[largest]) {
29+
largest = i;
30+
}
31+
}
32+
swap(ptr+smallest, ptr+largest);
33+
print(arr, n);
34+
return 0;
35+
}

revstr.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdbool.h>
4+
int main() {
5+
char str[50];
6+
scanf("%[^\n]s", str);
7+
int len = strlen(str);
8+
char str2[len];
9+
for (int i=0; i<len; i++) {
10+
str2[i] = str[len-i-1];
11+
}
12+
str2[len] = '\0';
13+
printf("%s", str2);
14+
return 0;
15+
}
16+
17+
//palindrome checker
18+
19+
#include <stdio.h>
20+
#include <string.h>
21+
#include <stdbool.h>
22+
int main() {
23+
char str[50];
24+
scanf("%[^\n]s", str);
25+
int len = strlen(str);
26+
char str2[len];
27+
for (int i=0; i<len; i++) {
28+
str2[i] = str[len-i-1];
29+
}
30+
31+
int cnt = 1;
32+
33+
for (int i=0; i<len; i++) {
34+
if (str2[i] != str[i]) {
35+
cnt = 0;
36+
}
37+
}
38+
if (cnt == 0) {
39+
printf("not a palindrome");
40+
} else printf("is a palindrome");
41+
return 0;
42+
}

strcmp.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
void findLength(char str[]) {
4+
int length = 0;
5+
while (str[length] != '\0') {
6+
length++;
7+
}
8+
if (str[length - 1] == '\n') {
9+
length--;
10+
}
11+
printf("Length of the string '%s' is: %d\n", str, length);
12+
}
13+
void compareStrings(char str1[], char str2[]) {
14+
int result = strcmp(str1, str2);
15+
if (result == 0) {
16+
printf("The strings '%s' and '%s' are equal.\n", str1, str2);
17+
} else {
18+
printf("The strings '%s' and '%s' are not equal.\n", str1, str2);
19+
}
20+
}
21+
int main() {
22+
char str1[100], str2[100];
23+
printf("Enter the first string: ");
24+
fgets(str1, sizeof(str1), stdin);
25+
printf("Enter the second string: ");
26+
fgets(str2, sizeof(str2), stdin);
27+
findLength(str1);
28+
findLength(str2);
29+
compareStrings(str1, str2);
30+
return 0;
31+
}

struc union/airline.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#define MAX_FLIGHTS 20
4+
#define MAX_CODE_LENGTH 4
5+
struct Flight {
6+
char startingPoint[MAX_CODE_LENGTH];
7+
char destination[MAX_CODE_LENGTH];
8+
int startingTime;
9+
int arrivalTime;
10+
int seats;
11+
};
12+
void processQuery(struct Flight flights[], int numFlights,
13+
char queryStart[], char queryDest[]) {
14+
for (int i = 0; i < numFlights; i++) {
15+
if (strcmp(flights[i].startingPoint, queryStart)
16+
== 0 &&
17+
strcmp(flights[i].destination, queryDest) ==
18+
0) {
19+
if (flights[i].seats > 0) {
20+
flights[i].seats--;
21+
printf("Flight found: %s -> %s, Starting: %d, Arrival: %d, Seats left: %d\n",
22+
flights[i].startingPoint,
23+
flights[i].destination,
24+
flights[i].startingTime,
25+
flights[i].arrivalTime, flights[i].seats);
26+
return;
27+
}
28+
}
29+
}
30+
printf("Sorry, no available flight found from %s to %s\n", queryStart, queryDest);
31+
}
32+
int main() {
33+
struct Flight flights[MAX_FLIGHTS];
34+
int numFlights = 20;
35+
for (int i = 0; i < numFlights; i++) {
36+
printf("Enter details for flight %d:\n", i + 1);
37+
printf("Starting point: ");
38+
scanf("%s", flights[i].startingPoint);
39+
printf("Destination: ");
40+
scanf("%s", flights[i].destination);
41+
printf("Starting time: ");
42+
scanf("%d", &flights[i].startingTime);
43+
printf("Arrival time: ");
44+
scanf("%d", &flights[i].arrivalTime);
45+
printf("Seats: ");
46+
scanf("%d", &flights[i].seats);
47+
}
48+
char queryStart[MAX_CODE_LENGTH];
49+
char queryDest[MAX_CODE_LENGTH];
50+
printf("Enter query in the form 'STARTING POINT-DESTINATION' (or 'exit' to stop):\n");
51+
while (1) {
52+
printf("Query: ");
53+
scanf("%s", queryStart);
54+
if (strcmp(queryStart, "exit") == 0) break;
55+
scanf("%s", queryDest);
56+
processQuery(flights, numFlights, queryStart,
57+
queryDest);
58+
}
59+
return 0;
60+
}

0 commit comments

Comments
 (0)