Yucky Homework Help, pweese

This is a discussion on Yucky Homework Help, pweese within the C Programming forums, part of the General Programming Boards category; Here is what I have to write for my stupid "C Programming" class in college. Ugh. If anyone can help, ...

  1. #1
    Unregistered
    Guest

    Question Yucky Homework Help, pweese

    Here is what I have to write for my stupid "C Programming" class in college. Ugh. If anyone can help, it would be much appreciated.

    Write a C program that reads a sentence (i.e. unknown number of characters
    ending with a period '.') and prints each word in the sentence on a separate line.
    The program should also print the length of each word. At the end, it will print the
    number of characters in the longest word. The sentence must start with a non -blank
    character and each word must be separated with exactly one blank character. (For
    this program a word is a sequence of any non-blank characters). There shouldn’t be
    any spaces between the last word and the period that ends the sentence. Your
    program should read the sentence one character at a time.

    Sample Run 1:
    Type the sentence: What a lovely day.

    Output:

    What 5 characters.
    a 1 character.
    lovely 6 characters.
    day 3 characters.
    The longest word in the sentence is 6 characters long.

    (Put a tab character between the words and the count of characters in the output)

    Requirements:

    Your program must include a function that reads a single word, prints the word
    and the number of characters, and returns the number of characters. Use the
    following function header:

    int read_and_count()

    (Hint: The function should continue reading and printing characters one by one
    until it re ads a blank (‘ ‘) or a period (‘.’). Then it should print and return the count
    to the main function. You must think of a way of how to inform the main function
    when the last word is read and printed)
    Your main function should call the function read_and_count() properly so that it
    can use the value that the function returns to find the number of characters in the
    longest word.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Here is what I have to write for my stupid...
    With the emphasis on "I have to write...". We will help but you need to make a real effort.

  3. #3
    Unregistered
    Guest

    Unhappy

    I have been making effort in this class. I started the class with a huge interest in C, but the teacher has turned me off to it very much. Sorry for my POed attitude. Im not looking for you to write the entire source for me, just little hints on how i could construct it. Thank you.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Sure you've atempted to try just a little. Post some code or no help. Read the homework announcement at the top of each board. Come here for problem solving not a quick answer.

    Feed a man for a day, he'll come back.
    Teach him how to hunt, never hear from him again.
    The world is waiting. I must leave you now.

  5. #5
    Unregistered
    Guest
    i also have the same assignment..and this is what i have so far...i dont think im on the right track..but at least its something, i want to get rid of the arrays, the pointers and the functions from the string.h library. i think it can be more basic than this:

    #include <stdio.h>
    #include <string.h>

    int read_and_count(char *str)
    {
    char wrd[256]; int p, s, l;

    p=(int)(strstr(str, ".")-str); if(p<0) p=-1;
    s=(int)(strstr(str, " ")-str); if(s<0) s=-1;
    if(p<s && p!=-1) s=-1;
    if(s =0 && s==-1) l=p;
    else l=s;
    strncpy(wrd, str, l);
    wrd[l]='\0';
    printf("%s\t%i characters.\r\n", wrd, l);
    if(p!=-1 || (p==-1 && s==-1)) return l;
    s=read_and_count(str+(l+1));
    return (s>l)?s:l;}void main(){ char str[4096];
    int len;
    // Fix this Part that reads the sentence
    printf("Enter a Sentence: ");
    scanf("%s", str);
    //////////////////////////////////////////////
    len=read_and_count(str);
    printf("\r\nThe longest word in the sentence is %i characters long.\r\n", len);
    }
    any help will be greatly appreciated!! please :-( i've tried..

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Try
    Code:
    int read_and_count(char *str) 
    { 
    char wrd[256];
    int iSentance=0,iWord=0,iMax=0;
    
    while(str[iSentance]!='.')//or'\0'
    {
       if(str[iSentance]!=' ') 
       {
           strcat(wrd[iWord],str[iSentance]);//add to the end of the wrd array
           iWord++;
           iSentance++;
       }
       else//is a space
       {
       //so print the wrd array and the index iWord
       //ADD THE PRINT LINE HERE
          iMax=max(iMax,iWord);//find the longest word
          iWord=0;//reset the word counter
          iSentance++;//add for the space
       }
    }
    return iMax;
    }
    This is not 100%. (I just typed it in) but is an idea.
    Last edited by novacain; 10-17-2001 at 12:57 AM.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Watch the

    if(s =0 && s==-1) l=p;

    This will not tell you if s is equal to zero it will set s to zero. It will never be -1 so the if will always be FALSE. (let alone the fact that a variable can not be 0 AND -1)

    A very common typo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 04:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 12:48 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21