Ubuntu 22.4:
/** * @file AnalyzingText.h * @brief Analyzing text * @author geovindu,Geovin Du,涂聚文 (geovindu@163.com) * ide: vscode c11,c17 Ubuntu 22.4 * @version 0.1 * @date 2023-11-05 * * @copyright Copyright (c) 站在巨人的肩膀上 Standing on the Shoulders of Giants 2023 * */ #ifndef ANALYZINGTEXT_H_ #define ANALYZINGTEXT_H_ #include <stdio.h> #include <string.h> #include <stdbool.h> #define TEXT_LEN 10000 // Maximum length of text #define BUF_SIZE 100 // Input buffer size #define MAX_WORDS 500 // Maximum number of different words #define WORD_LEN 12 // Maximum word length /** * @brief 计算英文单词出现的次数 * */ void AnalyText(); #endif
/** * ***************************************************************************** * @file AnalyzingText.c * @brief Analyzing text * @author geovindu,Geovin Du,涂聚文 (geovindu@163.com) * ide: vscode c11,c17 Ubuntu 22.4 * @date 2023-11-01 * @copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants * ***************************************************************************** */ #include "include/AnalyzingText.h" /** * @brief 计算英文单词出现的次数 * */ void AnalyText() { char delimiters[] = " \n\".,;:!?)("; // Word delimiters char text[TEXT_LEN] = ""; // Stores the complete text char buf[BUF_SIZE]; // Stores one input line char words[MAX_WORDS][WORD_LEN]; // Stores words from the text int nword[MAX_WORDS] = {0}; // Number of word occurrences int word_count = 0; // Number of words stored printf("Enter text on an arbitrary number of lines."); printf("\nEnter an empty line to end input:\n"); // Read an arbitrary number of lines of text while(true) { // An empty string containing just a newline // signals end of input fgets(buf, BUF_SIZE, stdin); if(buf[0] == '\n') break; // Concatenate new string & check if we have space for latest input //strcat_s(text, TEXT_LEN, buf) size_t tlen=TEXT_LEN; char *ppd=strncat(text, buf,tlen); if(strlen(text)==0) { printf("Maximum capacity for text exceeded. Terminating program.\n"); return 1; } } // Find the first word size_t len = TEXT_LEN; char *duptr = NULL; //char* pWord = strtok_s(text, &len, delimiters, &ptr); // Find 1st word //win //char* pWord = strtok_s(text,delimiters, &duptr); char* pWord = strtok(text,delimiters); //strtok(text,delimiters); // use this line instead, for Microsoft compiler: //char* pWord = strtok_s(text, delimiters, &ptr); // Find 1st word if(pWord==NULL) { printf("No words found. Ending program.\n"); return 1; } //win //strcpy_s(words[word_count], WORD_LEN, pWord); strcpy(words[word_count],pWord); ++nword[word_count++]; // Find the rest of the words bool new_word = true; // False for an existing word while(true) { //pWord = strtok_s(NULL, &len, delimiters, &ptr); // Find subsequent word //win //pWord = strtok_s(NULL, delimiters, &duptr); pWord = strtok(NULL, delimiters); // use this line instead, for Microsoft compiler: //pWord = strtok_s(NULL, delimiters, &ptr); // Find subsequent word if(!pWord) break; // NULL ends tokenizing // Check for existing word for(int i = 0 ; i < word_count ; ++i) { if(strcmp(words[i], pWord) == 0) { ++nword[i]; new_word = false; } } if(new_word) // True if new word { //strcpy_s(words[word_count], WORD_LEN, pWord); // Copy to array strcpy(words[word_count], pWord); ++nword[word_count++]; // Increment count and index } else new_word = true; // Reset new word flag if(word_count > MAX_WORDS - 1) { printf("Capacity to store words exceeded.\n"); return 1; } } // List the words for(int i = 0; i < word_count ; ++i) { printf(" %-13s %3d", words[i], nword[i]); if((i + 1) % 4 == 0) printf("\n"); } printf("\n"); }
输出:
windows 10
/** * ***************************************************************************** * @file AnalyzingText.h * @brief Analyzing text * @author geovindu,Geovin Du,涂聚文 (geovindu@163.com) * ide: vscode c11,c17 windows 10 * @date 2023-11-01 * @copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants * ***************************************************************************** */ #define __STDC_WANT_LIB_EXT1__ 1 #ifndef ANALYZINGTEXT_H_ #define ANALYZINGTEXT_H_ #include <stdio.h> #include <string.h> #include <stdbool.h> #define TEXT_LEN 10000 // Maximum length of text #define BUF_SIZE 100 // Input buffer size #define MAX_WORDS 500 // Maximum number of different words #define WORD_LEN 12 // Maximum word length /** * @brief 计算英文单词出现的次数 * */ void AnalyText(); #endif
/** * ***************************************************************************** * @file AnalyzingText.c * @brief Analyzing text * @author geovindu,Geovin Du,涂聚文 (geovindu@163.com) * ide: vscode c11,c17 windows 10 * @date 2023-11-01 * @copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants * ***************************************************************************** */ #include "include/AnalyzingText.h" /** * @brief 计算英文单词出现的次数 * */ void AnalyText() { char delimiters[] = " \n\".,;:!?)("; // Word delimiters char text[TEXT_LEN] = ""; // Stores the complete text char buf[BUF_SIZE]; // Stores one input line char words[MAX_WORDS][WORD_LEN]; // Stores words from the text int nword[MAX_WORDS] = {0}; // Number of word occurrences int word_count = 0; // Number of words stored printf("Enter text on an arbitrary number of lines."); printf("\nEnter an empty line to end input:\n"); // Read an arbitrary number of lines of text while(true) { // An empty string containing just a newline // signals end of input fgets(buf, BUF_SIZE, stdin); if(buf[0] == '\n') break; // Concatenate new string & check if we have space for latest input if(strcat_s(text, TEXT_LEN, buf)) { printf("Maximum capacity for text exceeded. Terminating program.\n"); return 1; } } // Find the first word size_t len = TEXT_LEN; char *ptr = NULL; //char* pWord = strtok_s(text, &len, delimiters, &ptr); // Find 1st word char* pWord = strtok_s(text,delimiters, &ptr); // use this line instead, for Microsoft compiler: //char* pWord = strtok_s(text, delimiters, &ptr); // Find 1st word if(!pWord) { printf("No words found. Ending program.\n"); return 1; } strcpy_s(words[word_count], WORD_LEN, pWord); ++nword[word_count++]; // Find the rest of the words bool new_word = true; // False for an existing word while(true) { //pWord = strtok_s(NULL, &len, delimiters, &ptr); // Find subsequent word pWord = strtok_s(NULL, delimiters, &ptr); // use this line instead, for Microsoft compiler: //pWord = strtok_s(NULL, delimiters, &ptr); // Find subsequent word if(!pWord) break; // NULL ends tokenizing // Check for existing word for(int i = 0 ; i < word_count ; ++i) { if(strcmp(words[i], pWord) == 0) { ++nword[i]; new_word = false; } } if(new_word) // True if new word { strcpy_s(words[word_count], WORD_LEN, pWord); // Copy to array ++nword[word_count++]; // Increment count and index } else new_word = true; // Reset new word flag if(word_count > MAX_WORDS - 1) { printf("Capacity to store words exceeded.\n"); return 1; } } // List the words for(int i = 0; i < word_count ; ++i) { printf(" %-13s %3d", words[i], nword[i]); if((i + 1) % 4 == 0) printf("\n"); } printf("\n"); }
输出: