c: Analyzing text in window and Ubuntu
Ubuntu 22.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /** * @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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | /** * ***************************************************************************** * @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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /** * ***************************************************************************** * @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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | /** * ***************************************************************************** * @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" ); } |
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-11-05 CSharp: Factory Method Pattern in donet 6
2009-11-05 Dijkstra's Shortest Path Algorithm(最短路径算法)