diff --git a/HighestFreq.c.txt b/HighestFreq.c.txt new file mode 100644 index 0000000..e9ff4e5 --- /dev/null +++ b/HighestFreq.c.txt @@ -0,0 +1,61 @@ +/* + * C Program To Find the Highest Frequency Character in a String + */ +#include +#include + +char string1[100], visited[100]; +int count[100] = {0}, flag = 0; + +void main() +{ + int i, j = 0, k = 0, l, max, index; + + printf("Enter a string : "); + scanf("%[^\n]s", string1); + + l = strlen(string1); + + for (i = 0; i < l; i++) + { + if (i == 0) + { + visited[j++] = string1[i]; + count[j - 1]++; + } + else + { + for (k = 0; k < j; k++) + { + if (string1[i] == visited[k]) + { + count[k]++; + flag = 1; + } + } + if (flag == 0) + { + visited[j++] = string1[i]; + count[j - 1]++; + } + flag = 0; + } + } + + for (i = 0; i < j; i++) + { + if ((i == 0) && (visited[i] != ' ')) + { + max = count[i]; + continue; + } + if ((max < count[i]) && (visited[i] != ' ')) + { + max = count[i]; + index = i; + } + } + + printf("\nMax repeated character in the string = %c ", visited[index]); + printf("\nIt occurs %d times", count[index]); +} diff --git a/MatrixOperations.c b/MatrixOperations.c new file mode 100644 index 0000000..6ad2d78 --- /dev/null +++ b/MatrixOperations.c @@ -0,0 +1,110 @@ +#include + +void createDiagonalMatrix(int n) { + int matrix[n][n]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + matrix[i][j] = (i == j) ? 1 : 0; + } + } + + printf("Diagonal Matrix:\n"); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + printf("%d ", matrix[i][j]); + } + printf("\n"); + } +} + +void createSquareMatrix(int size) { + int matrix[size][size]; + + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + printf("Enter element at position (%d, %d): ", i, j); + scanf("%d", &matrix[i][j]); + } + } + + printf("Square Matrix:\n"); + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + printf("%d ", matrix[i][j]); + } + printf("\n"); + } +} + +int sumMatrixElements(int rows, int cols) { + int matrix[rows][cols]; + int totalSum = 0; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + printf("Enter element at position (%d, %d): ", i, j); + scanf("%d", &matrix[i][j]); + totalSum += matrix[i][j]; + } + } + + printf("Custom Matrix:\n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + printf("%d ", matrix[i][j]); + } + printf("\n"); + } + + return totalSum; +} + +int main() { + while (1) { + printf("Menu:\n"); + printf("1: Enter numbers diagonally\n"); + printf("2: Create a particular sized square matrix\n"); + printf("3: Calculate sum of elements in matrix\n"); + printf("4: Exit\n"); + + int choice; + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: { + int size; + printf("Enter matrix size: "); + scanf("%d", &size); + createDiagonalMatrix(size); + break; + } + case 2: { + int size; + printf("Enter matrix size: "); + scanf("%d", &size); + createSquareMatrix(size); + break; + } + case 3: { + int rows, cols; + printf("Enter number of rows: "); + scanf("%d", &rows); + printf("Enter number of columns: "); + scanf("%d", &cols); + int totalSum = sumMatrixElements(rows, cols); + printf("Sum of elements in the matrix: %d\n", totalSum); + break; + } + case 4: { + printf("Exiting...\n"); + return 0; + } + default: + printf("Invalid choice! Please select a valid option.\n"); + } + } + + return 0; +} diff --git a/string operation 1.c.txt b/string operation 1.c.txt new file mode 100644 index 0000000..bbb0c8d --- /dev/null +++ b/string operation 1.c.txt @@ -0,0 +1,30 @@ +#include + +// Funtion removing spaces from string +char * removeSpacesFromStr(char *string) +{ + // non_space_count to keep the frequency of non space characters + int non_space_count = 0; + + //Traverse a string and if it is non space character then, place it at index non_space_count + for (int i = 0; string[i] != '\0'; i++) + { + if (string[i] != ' ') + { + string[non_space_count] = string[i]; + non_space_count++;//non_space_count incremented + } + } + + //Finally placing final character at the string end + string[non_space_count] = '\0'; + return string; +} + +int main() +{ + char string[] = "Edpresso: Dev -Shot"; + printf("%s \n",string); + printf("%s",removeSpacesFromStr(string)); + return 0; +} \ No newline at end of file