简体   繁体   中英

Word length frequency in C Program

I am trying to write a simple C program to output the length of words and output their frequencies. For example, if the user inputs "hey" my program would output Length of word: 3 Occurrences 1, and so on with a larger string inputted. I just cannot seem to loop it properly. I thought of setting both counters when a delimiter is seen to count both the length of the word at the time and its occurrence but I have not found a way for it to work. How can I fix my loop? My code is below. I'd appreciate any help. I should include my program only runs correctly for one word inputted but not a whole sentence or multiple sentences.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
const char delim[] = ", . - !*()&^%$#@<> ? []{}\\ / \"";
const int n_delim = 31;
#define SIZE 1000

int is_delim(int c);
int main(){
    char string[SIZE];
    int wordlength = 0, wl[SIZE];
    int word = 0, i;


    printf("Enter your input string:");
    fgets(string, SIZE, stdin);
    string[strlen(string) - 1] = '\0';

printf("Word Length\tCount\n");
    int seen = 0;
    int l;
    for (i = 0; i < strlen(string); i++){
        if (is_delim(string[i])){
            wl[word++] = wordlength;
            l = wordlength;
            seen++;
            printf("%d\t\t%d\n", l, seen);
            wordlength = 0;
        }
        wordlength++;

    }
    return 0;
}

int is_delim(int c){
    register int i;
    for (i = 0; i < n_delim; i++)
        if (c == delim[i]) return 1;
    return 0;
}

The trick is that wl[n] holds the count of words of length n. Also, you don't need to keep calling strlen() on every iteration, just check for the zero byte at the end. The optimizer will do this for you, if you enable it. The odd-looking for(;1;) is so that the loop counts the final word, which is terminated by the zero byte.

memset(wl,0,sizeof(wl));
for(wordStart=maxLength=i=0;1;i++) {
  if(is_delim(string[i]) || string[i]==0) {
    int wordLength= i-wordStart;
    if(wordLength>0)
      wl[wordLength]++;
    if(wordLength>maxLength)
      maxLength= wordLength;
    wordStart= i+1;
  }
  if(string[i]==0)
    break;
}

for(i=1;i<=maxLength;i++) {
  if(wl[i]>0) {
    printf("%d words of length %d.\n",wl[i],i);
  }
}

You really should use strtok for this. Right now, you never compare the last string with the current one so you can't tell them apart. You can use strcmp for this. Finally instead of manually testing the length of the string you should use strlen . Here is how your loop could look like

int seen = 0;

pch = strtok(string, delim);
last = pch;
while(pch != NULL) {
  if(strcmp(last, pch) != 0) {
    printf("%s:\t%d\t\t%d\n", last, (int)strlen(last), seen);
    seen = 1;
  }else {
    seen++;
  }
  last = pch;
  pch = strtok(NULL, delim);
}
printf("%s:\t%d\t\t%d\n", last, (int)strlen(last), seen);

Note, you should set the variable seen to 0 before the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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