简体   繁体   English

计算字符串中的字符时避免额外打印

[英]Avoiding extra printing when counting characters in a string

For my CS class we have to code in C the following program:对于我的 CS class,我们必须在 C 中编写以下程序:

For a txt file, count how many letters of the alphabet begin each word of the file.对于 txt 文件,计算文件中每个单词开头的字母数。 For example:例如:

  • t is used 5 times at the beginning of a word t 在词首使用 5 次
  • e is used 4 times at the beginning of a word e 在单词的开头使用了 4 次
  • etc ETC

The code I wrote does meet this but my method does this each time with each letter even if we have already counted it, I've included in it some way to sort of numb this down but it doesn't work, it just counts them less times.我写的代码确实符合这一点,但我的方法每次对每个字母都这样做,即使我们已经计算过了,我已经在其中包含了某种方式来麻木它,但它不起作用,它只是计算它们次数少。

Can anyone give me an idea to fix this?谁能给我一个解决这个问题的想法? How should I approach it?我应该如何处理它? I tried removing said character after reading but I just end up erasing the entire string instead.我尝试在阅读后删除所述字符,但最终我只是删除了整个字符串。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 64
#define LINES 64
#define file_location "text.txt"
int main(int argc, const char * argv[]) {
    char text[SIZE][LINES];
    char text_buff[SIZE+2];
    char word_arr[SIZE];
    int line_counter=0;
    
    
    
    FILE* file;
    file = fopen(file_location, "r");
    
    while(fgets(text_buff, SIZE, file)){
        text_buff[(strlen(text_buff)-1)]='\0';
        
        strcpy( text[line_counter],text_buff);
        line_counter++;
    }
    
    int i=0;
    printf("the file read is:\n");
    
    while(text[i][0]!='\0'){
        printf("%s\n",text[i]);
        i++;
        
    }
    int counter=0;
    
    for (int i=0; i<line_counter; i++) {
        for (int j=0; j<strlen(text[i]); j++) {
            if(isalpha(text[i][j]) && !isalpha(text[i][j-1])){
                word_arr[counter]=text[i][j];
                counter++;
            }
            
        }
    }
    printf("the letters starting each word are:\n");
    
    puts(word_arr);
    
    char reg[strlen(word_arr)];
    
    for (int i=0; i<strlen(word_arr); i++) {
        int counter_c=0;
        char buf = word_arr[i];
        for (int j=0; j< strlen(word_arr); j++) {
            if(buf == word_arr[j] && buf != reg[j]){
                counter_c++;
            }
        }
        int flag=1;
        
        for (int k=0; k<strlen(word_arr); k++) {
            if(reg[i]==buf){
                flag=0;
            }
        }
        
        if(flag==1){
          printf("%c has been used to start a word %d times\n",buf,counter_c);
        }
        
        reg[i]=buf;
    }
        
        
    

   
    
  
    
    
    
    return 0;
}



The output is: output 是:

t has been used to start a word 6 times
i has been used to start a word 1 times
a has been used to start a word 1 times
f has been used to start a word 3 times
f has been used to start a word 2 times
c has been used to start a word 1 times
t has been used to start a word 5 times
p has been used to start a word 1 times
t has been used to start a word 4 times
w has been used to start a word 2 times
n has been used to start a word 1 times
t has been used to start a word 3 times
w has been used to start a word 1 times
f has been used to start a word 1 times
t has been used to start a word 2 times
e has been used to start a word 1 times
o has been used to start a word 1 times
J has been used to start a word 1 times
t has been used to start a word 1 times


You should probably do something like this semipseudo:你可能应该做这样的半伪:

int histogram['z'-'a'+1] = {0}; // Room for whole alphabet

for each word in file: 
    histogram[lowercase(word[0]) - 'a']++;

After this, histogram['t'-'a'] will contain the number of words that starts with t or T .在此之后, histogram['t'-'a']将包含以tT开头的单词数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM