1234
unknown
plain_text
10 days ago
4.2 kB
4
Indexable
Never
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> // Function declarations void displayMenu(); char** readFromFile(const char* filename, int* wordCount); void processWords(char** words, int wordCount, char** sortedWords[4], int sortedCount[4]); void writeToFile(const char* filename, char** sortedWords[4], int sortedCount[4]); int main() { int choice; char inputFile[100], outputFile[100]; int wordCount = 0; char **words = NULL; char *sortedWords[4][100]; int sortedCount[4] = {0}; displayMenu(); printf("Choose an option (1 to execute or 0 to exit): "); scanf("%d", &choice); if (choice == 1) { // Input the names of input and output files printf("Enter the name of the input file: "); scanf("%s", inputFile); printf("Enter the name of the output file: "); scanf("%s", outputFile); // Reading from the file words = readFromFile(inputFile, &wordCount); if (words != NULL) { // Processing the data processWords(words, wordCount, sortedWords, sortedCount); // Writing the result to an output file writeToFile(outputFile, sortedWords, sortedCount); // Freeing dynamically allocated memory for (int i = 0; i < wordCount; i++) { free(words[i]); } free(words); } } return 0; } // Function to display the menu void displayMenu() { printf("===== Menu =====\n"); printf("1. Read words from file\n"); printf("2. Sort words by first letter (A, B, C, D)\n"); printf("3. Write result to file\n"); printf("0. Exit\n"); printf("================\n"); } // Function to read words from input file char** readFromFile(const char* filename, int* wordCount) { FILE *file = fopen(filename, "r"); if (file == NULL) { printf("Error opening the file.\n"); return NULL; } char buffer[100]; char **words = NULL; *wordCount = 0; // Read words from the file while (fscanf(file, "%s", buffer) != EOF) { words = (char**) realloc(words, (*wordCount + 1) * sizeof(char*)); words[*wordCount] = (char*) malloc((strlen(buffer) + 1) * sizeof(char)); strcpy(words[*wordCount], buffer); (*wordCount)++; } fclose(file); return words; } // Function to process words and sort them by their first letter void processWords(char** words, int wordCount, char** sortedWords[4], int sortedCount[4]) { for (int i = 0; i < wordCount; i++) { char firstLetter = toupper(words[i][0]); if (firstLetter == 'A') { sortedWords[0][sortedCount[0]++] = words[i]; } else if (firstLetter == 'B') { sortedWords[1][sortedCount[1]++] = words[i]; } else if (firstLetter == 'C') { sortedWords[2][sortedCount[2]++] = words[i]; } else if (firstLetter == 'D') { sortedWords[3][sortedCount[3]++] = words[i]; } } } // Function to write sorted words into output file void writeToFile(const char* filename, char** sortedWords[4], int sortedCount[4]) { FILE *file = fopen(filename, "w"); if (file == NULL) { printf("Error opening the output file.\n"); return; } // Writing words starting with 'A' fprintf(file, "Words starting with A:\n"); for (int i = 0; i < sortedCount[0]; i++) { fprintf(file, "%s\n", sortedWords[0][i]); } // Writing words starting with 'B' fprintf(file, "\nWords starting with B:\n"); for (int i = 0; i < sortedCount[1]; i++) { fprintf(file, "%s\n", sortedWords[1][i]); } // Writing words starting with 'C' fprintf(file, "\nWords starting with C:\n"); for (int i = 0; i < sortedCount[2]; i++) { fprintf(file, "%s\n", sortedWords[2][i]); } // Writing words starting with 'D' fprintf(file, "\nWords starting with D:\n"); for (int i = 0; i < sortedCount[3]; i++) { fprintf(file, "%s\n", sortedWords[3][i]); } fclose(file); }
Leave a Comment