Beginner C programming Please Help!

This is a discussion on Beginner C programming Please Help! within the C Programming forums, part of the General Programming Boards category; This program is going to be hawaiian word spell check, Written Hawaiian also has fairly simple spelling rules for determining ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    30

    Beginner C programming Please Help!

    This program is going to be hawaiian word spell check,
    Written Hawaiian also has fairly simple spelling rules for determining if a word is a valid word in the language (even if the meaning is unknown). They are:

    * All words contain only vowels and Hawaiian consonants.
    * All words end in a vowel.
    * Within a word, two consonants NEVER appear adjacent.

    Write a program which reads lines of Hawaiian text from a file (using redirection, so you will not need to prompt), and lists each word found on a separate line indicating whether it is a valid Hawaiian spelling or it is invalid. Any non-letter characters (e.g. white space or punctuation characters) in the file should be treated as delimiters, but otherwise ignored and not appear in the output.
    ex: it will read something like this:
    this is not hawaiian word.
    and print:
    this it is invalid
    is it is invalid
    not it is invalid
    hawaiian it is valid
    word it is invalid
    ================================================== ===================
    I have written a program called is_vowel and is_h_consonant to check if they are.
    I also have written a program called delimit_p to see if the input is a delimiter.
    This is the program I have so far, it does not work correctly.

    Code:
    main()
    {
            char ch;
            char last_ch;
            int inword;
            int hi;
           
            /*      we are not in the word at the beginning */
            inword = FALSE;
            /*      we assume everything is not hawaiian word       */
            hi = FALSE;
            /*      set last character as a vowel   */
            last_ch = 'a';
    
            /*      while there are more characters         */
            while((ch = getchar()) != EOF)
            {
    
            /*      if not in word and not a delimiter      */
            /*      then must be the beginnning of a word   */
                    if (!inword && !delimitp(ch))
                    {
                            inword = TRUE;          /*      we are in a word now    */
                    }
            if(inword)
            {
            if(is_vowel(ch))
            {       hi = TRUE; }
            else if(is_h_consonant(ch))
                    {       if((is_h_consonant(ch) && !is_h_consonant(last_ch)) || (!is_h_consonant(ch) && is_h_consonant(last_ch)))
                                    { hi = TRUE; }
                    }
            else if(delimitp(ch) && is_vowel(last_ch))
                    { hi = TRUE; }
            else { hi = FALSE ; }
            putchar(ch);
            }
    
                    last_ch = ch;
                    ch = getchar();
    
                    if (inword && delimitp(ch))             /* if in word and a delimiter   */
                    inword = FALSE;                 /* we are no longer in a word   */
                    if(hi) {printf("\t  It is valid!");}      /* if it is correct spelling, print it's valid  */
                    else {printf("\t It is invalid!"); }    /* if not, print it is invalid  */
                    putchar('\n');                         /* print new line character     */
                    hi = FALSE;
    }
    }
    Super thanks for whoever that will spend time helping me on this!!!

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Hmm. Well, first of all, I would highly recommend that you indent your code slightly better. It really does make things easier to spot. (Here is a nice guide on that.)

    What problems are you having? (I'm assuming that your functions for checking if a char is a vowel or consonant are correct.)
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Thank you!
    My problem is for example I input:"kaleo"
    it says
    k it is valid (because it's a consonant)
    l it is invalid(because it's a consonant too and right after one)
    e it is valid (it's a vowel)

    my is_h_consonant and is_vowel works perfect, I have a driver to test it.
    My program first skip the 2nd, 4th, 6th ... words.
    also it reads characters instead of the whole word.

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    So, it's skipping every other character for consonants?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    it's skipping every other character, doesn't matter what it is..........
    i wish it would understand me more... >_<
    so frustrating!

  6. #6
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Code:
                    if (!inword && !delimitp(ch))
    What is the function delimitp(char)?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    this is where delimitp located.

    Code:
    #include <stdio.h>
    #include "tfdef.h"
    #include "chrutil.h"
    
    /*      Function returns TRUE if c is a delimiter, otherwise FALSE      */
    int delimitp(char c)
    {
            if(whitep(c) || punctp(c))
                    return TRUE;
            return FALSE;
    }
    
    /*      Function returns TRUE if c is a white space, else FALSE         */
    int whitep(char c)
    {
            if(c == '\n' || c == '\t' || c == ' ')
                    return TRUE;
            return FALSE;
    }
    
    /*      Function returns TRUE if c is a punctuation, else FALSE         */
    int punctp(char c)
    {
            if (c == '.' || c == ',' || c == ';' || c == '?' || c == '!')
                    return TRUE;
            return FALSE;
    }

  8. #8
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Code:
    while((ch = getchar()) != EOF)
            {
    
            /*      if not in word and not a delimiter      */
            /*      then must be the beginnning of a word   */
                    if (!inword && !delimitp(ch))
                    {
                            inword = TRUE;          /*      we are in a word now    */
                    }
            if(inword)
            {
            if(is_vowel(ch))
            {       hi = TRUE; }
            else if(is_h_consonant(ch))
                    {       if((is_h_consonant(ch) && !is_h_consonant(last_ch)) || (!is_h_consonant(ch) && is_h_consonant(last_ch)))
                                    { hi = TRUE; }
                    }
            else if(delimitp(ch) && is_vowel(last_ch))
                    { hi = TRUE; }
            else { hi = FALSE ; }
            putchar(ch);
            }
    Where do you set last_ch?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Code:
    main()
    {
            char ch;
            char last_ch;
            int inword;
            int hi;
           
            /*      we are not in the word at the beginning */
            inword = FALSE;
            /*      we assume everything is not hawaiian word       */
            hi = FALSE;
            /*      set last character as a vowel   */
            last_ch = 'a';

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Your output statements, that say whether a word is valid or not, are part of the main while loop and will print something every iteration: Once inword is true it is never made false. That will need to be fixed.

    Figure out the code that can toggle inword. Perhaps:
    Code:
    if (is_vowel(last_ch) && delimitp(ch)) {
       inword = FALSE;
       continue; /* this line could be eliminated if this if-statement is the last one */
    }
    And when inword is false you can print the output based on hi.

    If you are in a word, I prefer to assume you are in a valid hawaiian word until it breaks one of your rules.

  11. #11
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    . . . you need to set it in the loop as well. That's my point.

    Otherwise, it's always checking against a vowel, no?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Quote Originally Posted by Nightowl View Post
    . . . you need to set it in the loop as well. That's my point.

    Otherwise, it's always checking against a vowel, no?
    Line 38, Op's post... although i did indent the program before looking.
    Last edited by whiteflags; 03-19-2009 at 11:52 PM.

  13. #13
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Well, yeah. I should probably learn to do that.

    The problem is, I guess, that it's skipping characters, then?

    Kind of hard to do, considering you're not working with an array here . . .

    Is it just me, or do you have two getchar()s?
    Code:
            while((ch = getchar()) != EOF)
    Code:
                    ch = getchar();
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    >> Well, yeah. I should probably learn to do that.
    It helps

    >> The problem is, I guess, that it's skipping characters, then?
    One of them, I think I spotted the other one.

    >> Kind of hard to do, considering you're not working with an array here . . .
    I'm actually a bit impressed though. OP may not know it, but he's attempting to use a finite state machine.

    >> Is it just me, or do you have two getchar()s?
    You're not seeing things. You correctly pointed that out.

  15. #15
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    I'm actually a bit impressed though. OP may not know it, but he's attempting to use a finite state machine.
    Me? I'm very impressed.

    I think the double getchar()s are the main problem with skipping here.

    Change that around a little. Perhaps using a while(1) loop, and either break or continue based on the input from the second getchar().
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 12:48 PM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 03:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 06:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

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