如何逐行枚举从文件中读取的文本,并找到 C 中字符和单词最多的行
How can I enumerate text read from a file line by line, and find the line with the most characters and words in C?
我目前正在编写一个程序,该程序应该从文本文件中读取(通过命令行提供给程序(,然后枚举并打印出每一行,并提供有关单词,字符和行数的信息,以及有关哪一行具有最多字符/单词的信息。
到目前为止,我已经能够让它计算单词、字符和行的数量,但很难找到一种方法来枚举和打印每一行并找到哪些行的字符/单词最多。
这是我的代码
#include <stdio.h>
#define IN 1
#define OUT 0
int main( )
{
int line, word, character, state, place, flag
maxW, maxC;
state = OUT;
line = word = character = 0;
while( (place = getchar()) != EOF ) {
++character;
flag = IN;
printf("%c", place);
if( place == 'n' ) {
++line;
printf("%d: ", (line + 1));
flag = IN;
}
if(flag == IN) {
}
if( place == ' ' || place == 'n' || place == 't' )
state = OUT;
else if( state == OUT ) {
state = IN;
++word;
}
}
printf("%d lines, %d words, & %d characters.n", line, word, character);
return 0;
}
创建本地最高字符数和当前字符数
int highestCharCount = 0;
int currentCharCount = 0;
跟踪字符数
++currentCharCount;
每个循环检查字符数是否高于最高字符数,如果是,则更改最高字符数。
If(currentCharCount > highestCharCount){
highestCharCount = currentCharCount ;
}
在获得新行字符的同时重置它。
if( place == 'n' ) {
currentCharCount = 0;
++line;
printf("%d: ", (line + 1));
flag = IN;
}
这样,您最终将获得最高的字符数。
相关文章:
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 比较"A"、"C"、"G"'T'字符的最快方法
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- 字符串中字符的最大频率
- 使用哈希 C++ 查找具有 k 个不同/唯一字符的最长子字符串
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 使用getline,c 中的数组以相反的方式将所有字符单词转动
- 查找单词最多的行并将其输出到文本文件中.C++
- 查找不包含重复字符的最长子字符串
- 如何在C++中获取字符中最不重要的部分
- 替换由谓词确定的 std::string 中的字符的最 stl-ish 方法是什么
- 使用 getchar() 输入字符的最有效方法,就像 cin 中没有换行符和空格字符一样C++
- 如何逐行枚举从文件中读取的文本,并找到 C 中字符和单词最多的行
- 模板函数如何获得整数或字符的最大值
- 从C++字符串中删除不允许使用的字符的最优雅、最有效的方法
- 为字符C++指定最小值
- 创建 2D 数组并在每个数据槽中填充不同字符的最简单方法是什么?
- 一次移动多个未签名字符以便它们从一个字符流向另一个字符的最快方法是什么?
- 位操作 - 如何在C++中获取字符中最不重要的 3 位
- 在c++中,判断unicode字符字符串是否具有唯一字符的最有效方法是什么?