Thread: help please. to list words and.....

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    Red face help please. to list words and.....

    Hi there.
    i got this program to do 4 an assignment...i finished up to a part, but i can't seem to do the final bit....the program is supposed to display the word of each sentence as a list on the screen and stating which sentece the words belong to.eg

    sentence 1
    word
    word
    sentence2
    word
    word .....and so on

    if the paragraph has puncutation characters it should not be listed in the list.

    i know'i'm asking too much but i was trying all nite last nite and all day today to figure it out........PLEASE HELP
    thank you




    #include <stdio.h>
    #define max_char 100

    main(){
    char para[max_char];
    int len_sent,i,count,unit;

    printf("Please enter a paragraph\n");
    fflush(stdin);
    gets(para);

    len_sent = strlen(para);
    count=0;

    for(i=0;i<len_sent;i++) {
    if (para[i]=='.'){
    count++;
    }
    }

    printf("the number of sentences are: %d\n", count );

    count=1;

    for(i=0;i<len_sent;i++){
    if (para[i]== ' ') {
    count++;
    }
    }
    printf("The number of words in the paragraph are %d\n",count);
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm well first of all I see evil old gets() in your program (step slowly away from the computer and it may not attack). Also , you are only checking for periods as far as punctuation goes. There are many ways of doing this sort of thing. I remember seeing this done in "The C Programming Language" a couple of times. If you need to keep track of the sentences then you could create a struct like

    Code:
    struct whatever {
        char *sentence;
        char **white_space_array;
    };
    
    //skeleton code
    void read(char *read_in_data, struct whatever *w) {
       int i, j;
    
       i = strlen(read_in_data);
       j = count_whitespace(read_in_data);
       w->sentence = (char *)malloc(i);
       if(!w->sentence)
            fprintf(stderr, "NULL allocation!\n");
        else
            strcpy(w->sentence, read_in_data);
        w->white_space_array = (char **)malloc(j * sizeof(char *));
    
        replace_all_whitespace_with_space_character(w->sentence);
        //now just use strtok or something to tokenize w->sentence and point w->white_space_array to each of the tokens.
    }
    Don't get mad because I didn't give specific code. It isn't my assignment, and nothing is gained from me doing everything for you.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when pasting code... see my signature for a link and an example.

    @RAY:
    Both of these are bad:
    >fflush(stdin);
    >gets(para);
    Do a board search for more information.

    You only need one loop. Read the data into your array (using fgets() instead of gets() ), then simply do something like this:

    Code:
    int Sentence = 1
    Write "Sentence %d\n", ++Sentence;
    For i = 0 to len(input)
        if input[i] == EndOfSentence 
            Write "Sentence %d\n", ++Sentence;
        end if
        if (input[i] is part of a word)
            Write input[i]
        end if
        else if (input[i] is an end of word marker)
            Write '\n'
        end if
    end for
    Also, the code you supplied doesn't do what your assignment asks, as it doesn't actually print any words.... but I'm sure you knew this already

    @master5001: Your code has a nasty little bug:
    >i = strlen(read_in_data);
    >w->sentence = (char *)malloc(i);
    >strcpy(w->sentence, read_in_data);
    You blew the array buffer by forgetting to include 1 extra byte for the null terminator.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM