Need Some Guidance

This is a discussion on Need Some Guidance within the C Programming forums, part of the General Programming Boards category; Hello I just started my C programming class and I am having trouble with an assignment. I was hoping someone ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    3

    Need Some Guidance

    Hello I just started my C programming class and I am having trouble with an assignment. I was hoping someone here can offer some assistance.

    The assignment is to write a complete program asking the user to enter a phrase and then compute the following:
    1. Total number of A's, E's, I's, O's, and U's, and print them.
    2. Total number of Blanks, and total number of lines, and print them.
    3. Total number of words and print them. (word is string of characters separated by blanks)

    Any help would be much appreciated

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    This is what I've managed so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        char string[256];
        int slenght;
        
        printf("Enter a phrase:\n");
        fgets(string, 256, stdin);
        slenght = strlen(string)-1;
        printf("You entered:\n%s", string);
        printf("Your phrase was %d characters long",slenght);
        
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,664
    So you have code that reads a string, computes its length, and prints out the string and its length.

    The thing is, your assignment is to also add some code that also computes
    1. Total number of A's, E's, I's, O's, and U's, and print them.
    2. Total number of Blanks, and total number of lines, and print them.
    3. Total number of words and print them. (word is string of characters separated by blanks)

    You haven't done any of those, and they are the central requirements of your homework.

    This site has a homework policy, this link.

    That policy essentially says "you must make an honest effort to do your own homework". You haven't provided any evidence of effort, so you can't expect help.

    End of guidance.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Thanks grumpy and your absolutely right. I have been working on this since I've posted my last post and have gotten much further. Now I actually have some specific questions lol. I'm having a bit of trouble figuring out a way to count words. I'm not looking for the straight answer, just hints so I can figure it out my self . Here is what I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char string [100];
    int slength;
    
    
    int search (char x);
    int main()
    {
        int finda;
        int finde;
        int findi;
        int findo;
        int findu;
        int findspace;
        
        printf("Enter a phrase.\n");
        fgets(string, 100, stdin);
        printf("You entered %s\n",string);
        slength = strlen(string)-1;
        finda = search ('a') + search ('A');
        finde = search ('e') + search ('E');
        findi = search ('i') + search ('I');
        findo = search ('o') + search ('O');
        findu = search ('u') + search ('U');
        findspace = search (' ');
        printf("There were %d letter a's in your phrase\n", finda);
        printf("There were %d letter e's in your phrase\n", finde);
        printf("There were %d letter i's in your phrase\n", findi);
        printf("There were %d letter o's in your phrase\n", findo);
        printf("There were %d letter u's in your phrase\n", findu);
        printf("There were %d spaces in your phrase\n", findspace);
        
        return 0;
    }
    
    
    int search (char x)
    {
        int counter = 0;
        int stringpos;
        
        for(stringpos=0; stringpos<=slength; stringpos++)
        if(string[stringpos] == x)
        counter++;
        
        return counter;
    }

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    My name is Georgios Samaras. How many words and how many whitespaces? 5 and 4 respectively.

    Code:
    #include <stdio.h>             /* Header file for standard I/O library */
    
    int main(void)
    {
        char buffer[50];
        int i, spacesNo = 0;
        
        fgets(buffer, 50, stdin);
        
        /* Go through the whole sentence. */
        for(i = 0 ; i < 50 ; i++)
            /* Increment counter when you find a space. */
            if(buffer[i] == ' ')
                spacesNo++;
        
        printf("How many words? %d\n", spacesNo + 1);
        
        return 0;
    }
    But what when I have this: " Hello world ! " Words are two, but spaces are 4. In normal cases it would be one space of course. This has to do with your hw. You can however modify the code (which is something you have to get used to do) in order to check whether the character that comes after the space is a letter (isalpha).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    In other words, most often words are counted by analyzing the transitions from characters to white space. We will leave the implementation up to you.

  7. #7
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    One recommendation is to read in the characters one-by-one, much like you are doing right now. But instead of searching for one char at a time, read through the char's and every time you come across a vowel, increment it's counter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to gcc, need guidance
    By time4f5 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2011, 06:01 PM
  2. Need further guidance
    By jlimz in forum C Programming
    Replies: 3
    Last Post: 07-26-2010, 07:34 PM
  3. Guidance, please.
    By mattagrimonti in forum C Programming
    Replies: 2
    Last Post: 11-26-2008, 08:50 AM
  4. Need Guidance
    By hYph3n in forum C++ Programming
    Replies: 4
    Last Post: 02-29-2008, 02:09 PM
  5. Little guidance please...
    By Jayhawk_26 in forum C Programming
    Replies: 5
    Last Post: 10-09-2006, 02:27 AM

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