Question:
Write a C program that reads text from stdin and writes a word-length frequency table to stdout?
anonymous
2010-03-16 21:57:34 UTC
Wants us to use a struct for our table
Wants use strtok(), scanf(), and malloc()
Wants us to take care of all memory allocations ourselves.

I am stuck trying to implement this and need some guidance.

In particular I am confused with the difference between char *string; char string; and char[20] string;

And with how to use scanf();
especially in this assignment he wants us to use a while(scanf()) loop.
I tried getting my while scan f loop to work but to no avail.
I used char[20] string; followed by scanf("%s", string); and that worked but when i tried
char[20] string; and then a while(scanf("%s", string) that didn't work and
char[20] string; and then a while(scanf("%s", string == 1) that didn't work either

Additionally I think the format of the string reading the professor wants us to use is %as (i.e. scanf("%as", string)
I am not sure exactly what %as does or how to implement it correctly.

Thank you in advance

I am a previous Java user and am new to C so i need plenty of guidance as to which path to take and possible syntax.
Three answers:
cja
2010-03-17 05:37:58 UTC
You say you're supposed to use strtok, scanf, and malloc; strtok is the key here, I have no use for scanf or malloc in my solution. Look up strtok in your textbook or on-line somewhere, and learn how it works. If you use it correctly, it'll walk you through a C string word-by-word, which is exactly what you need. Getting a line of text from a file or from user input is easy, just use fgets. If you really need to use malloc, you can dynamically allocate the array of char used to store a line of input text, and you could also allocate the frequency table. So, study and understand what I've done below, and I think you can learn something. It should help you to get your code written, debugged, and running.



#include

#include



#define MAX_LINE_LEN 1024

#define MAX_WORD_LEN 10

#define WS " .\",:;-(){}[]<>/\\=+\t"



int main(int argc, char *argv[]) {

    FILE *fp;

    char line[MAX_LINE_LEN],*s;

    int freq[MAX_WORD_LEN+2] = { 0 }, len, i;

   

    if ((argc < 2) ||

            ((fp = fopen(argv[1],"r")) == NULL)) {

        printf("\nusage : %s \n",argv[0]);

        return -1;

    }

    while (fgets(line,MAX_LINE_LEN,fp) != NULL) {

        *strchr(line,'\n') = '\0';

        for (s = strtok(line,WS); s != NULL; s = strtok(NULL,WS)) {

            if ((len = strlen(s)) <= MAX_WORD_LEN) {

                freq[len]++;

            } else {

                freq[MAX_WORD_LEN+1]++;

            }

        }

    }

    printf("\nWord Length Frequency\n");

    printf("- - - - - - - - - - -\n");

    for (i = 1; i <= MAX_WORD_LEN; i++) {

        if (freq[i] > 0) {

            printf(" %3d %3d\n",i,freq[i]);

        }

    }

    if (freq[i] > 0) {

        printf(" > %d %3d\n",i-1,freq[i]);

    }

    puts("");

    fclose(fp);

    return 0;

}



#if 0



Sample run:



$ ./words6 text8.txt



Word Length Frequency

- - - - - -  - - - - -

     1          3

     2          6

     3          1

     4          7

     5          6

     6          5

     7          6

     8          1

    10          1

 > 10          1





Where:



$ cat text8.txt

Tiger Woods didn't just announce Tuesday that he

will return to competitive golf next month at the

Masters. He knocked over a bunch of dominos, punched

a button here, moved a needle there, raised an

eyebrow everywhere.



#endif
Cameron Belt
2010-03-16 22:07:48 UTC
Scanf functions should always have an ampersand before the variable name so it should look like this



scanf("%d",&IntegerOne);



you really should have put your code on here bc i dont know if you did that or not. but you need that ampersand to properly store the typed number under the variable name you want it be under. hope that helps
anonymous
2010-03-16 22:12:58 UTC
Place a bid request on rentacoder.com.



No one does homework for you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...