Program to count parts of a sentence

This is a discussion on Program to count parts of a sentence within the C Programming forums, part of the General Programming Boards category; I am trying to write a code that will count the parts of an inputted sentence (vowels, alpha, and keystrokes). ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19

    Program to count parts of a sentence

    I am trying to write a code that will count the parts of an inputted sentence (vowels, alpha, and keystrokes). I am not getting any errors when I debug, but my calculation keeps coming up identical for each category (and wrong) no matter what I input for the sentence. Please help! Thanks so much!

    Code:
     //This program is designed read an inputted sentence and count the keystrokes,
    //alpha characters, and vowels in the sentence and display each count as output. 
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h
    #include <string.h>
    
    int main(void)
    {
    char sentence[9999]; //Defines the character array for sentence input and analysis.
    int isdigit(int sentence); //Function returns non-zero if its argument is a digit between 0-9. Otherwise, zero is returned.
    int counter=0; //Counter variable.
    int keystrokes=0; //Keystrokes variable.
    int alpha_characters=0; //Alpha characters variable.
    int vowels=0; //Vowels variable.
    printf("Please input you sentence to be analyzed and hit enter to begin program.\n"); //Prompt for user to input sentence.
    scanf(" %s",sentence); //Keyboard input of sentence.
    for(counter=0;sentence[counter]!='\n';++counter) //For loop to analyze sentence parts.
    {
    if(sentence[counter]=='a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U') //Analyze lowercase and uppercase vowels in sentence. 
    {
    ++keystrokes; //Add to keystrokes count.
    ++alpha_characters; //Add to alpha characters count.
    ++vowels; //Add to vowels count.
    }
    elseif(isdigit(sentence[counter])==1) //Analyze all alpha characters in sentence.
    {
    ++keystrokes; //Add to keystrokes count.
    ++alpha_characters; //Add to alpha characters count.
    }
    else//Analyze all keystrokes in sentence.
    {
    ++keystrokes; //Add to keystrokes count.
    }
    }
    printf("Stephanie Johansen\tProgram #5\tCS604\n"); //Print header for output.
    printf("Keystrokes:\t\t %4i\n", keystrokes); //Print total Keystrokes output.
    printf("Alpha Characters:\t %4i\n", alpha_characters); //Print total Alpha Characters output.
    printf("Vowels:\t\t\t %4i\n", vowels); //Print total Vowels output.
    return 0; //Exit program.
    }
    Last edited by Salem; 02-20-2012 at 10:38 AM. Reason: removed lots of pointless tags - remember, it's copy/paste TEXT ONLY

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    if(sentence[counter]=='a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U')
    Nope, learn how to use logical operators. This will always evaluate correct. Instead, use
    Code:
    char tmp = toupper(sentence[counter]);
    if (tmp == 'A' || tmp == 'E' || tmp == 'I' || tmp == 'O' || tmp == 'U')
    Code:
    elseif(isdigit(sentence[counter])==1)
    I hope this was a typo.. (else if, not elseif)

    Code:
    int isdigit(int sentence); //Function returns non-zero if its argument
    You don't need to prototype functions in main, especially if they're already defined.

    Also, for convention/readability, indent correctly, lose the useless comments, and don't use colored/custom fonts in code tags.

    Edit: also, don't waste. Replace:

    Code:
    char sentence[9999];
    /* … */
    scanf(" %s",sentence);
    with
    Code:
    char *sentence = malloc(256);
    /* ... */
    fgets(sentence, 255, stdin);
    /* ... */
    free(sentence);
    Make these changes and we can help you further.
    Last edited by memcpy; 02-20-2012 at 10:30 AM.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Yes I have it as else if in my code, not sure why it lost the space in the cut-pase process. All my indentation in fine in my program, too. Another loss to the cut-paste process. Sorry for the "useless comments", I am in a programming course. We must comment every line. And the coloring comes from Visual Studios, not me.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Code:
     
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    
    int main(void)
    {
    char *sentence = malloc(256); 
    char tmp=toupper(sentence[counter]);
    int counter=0; 
    int keystrokes=0; 
    int alpha_characters=0; 
    int vowels=0; 
    
    printf("Please input you sentence to be analyzed and hit enter to begin program.\n"); 
    fgets(sentence,255,stdin); 
    free(sentence);
    
    for(counter=0;sentence[counter]!='\n';++counter) 
    {
             if(tmp=='a'||tmp=='A'||tmp=='e'||tmp=='E'||tmp=='i'||tmp=='I'||tmp=='o'||tmp=='O'||tmp=='u'||tmp=='U') 
                       {
                             ++keystrokes; 
                             ++alpha_characters; 
                             ++vowels; 
                       }
             else if(isdigit(sentence[counter])==1) 
                       {
                             ++keystrokes;
                             ++alpha_characters; 
                        }
             else
                        {
                             ++keystrokes; 
                         }
             }
    
    printf("Stephanie Johansen\tProgram #5\tCS604\n"); 
    printf("Keystrokes:\t\t %4i\n", keystrokes); 
    printf("Alpha Characters:\t %4i\n", alpha_characters); 
    printf("Vowels:\t\t\t %4i\n", vowels);
    
    return 0; 
    }
    

    Still not adding properly and giving identical output for all three categories

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    The toupper() function should be in the loop:

    Code:
    char tmp;
    /* ... */
    for (counter = 0; sentence[counter] != '\n'; counter++) /* counter++, not ++counter */
        tmp = toupper(sentence[counter]);
    Edit: No need to increment keystrokes every time, get rid of it and just print counter at the end.

    And, no need to check for both cases, that's what "toupper" does. Check this out: http://www.cplusplus.com/reference/clibrary/cctype/
    Last edited by memcpy; 02-20-2012 at 10:57 AM.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    With that update, I am getting "Debug Assertion Failed: Expression: (unsigned)(c+1)<=256" and "Error: a value of type "void*" cannot be used to initialize an entity of type "char*"

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Don't free the variable before you're done using it!

    Free when you're done counting.

    And if fgets is generating errors, you could try typecasting (I'm assuming that's the problem, you could tell me the line number).

    Code:
    fgets((void *)sentence, 255, stdin);

  8. #8
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Name:  screen1.jpg
Views: 323
Size:  124.7 KB

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    That looks like a MSVC++ issue, it compiles/runs fine on my machine...

  10. #10
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Ok, thanks. I will try to find another computer to run it on.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Ok, this is driving me crazy now. Got to a different computer. No debugging errors and the output numbers are at least all different from one another, but the counter is still way off. What am I missing here? I feel like this is such a straight-forward program...it should not be this difficult to trouble shoot!

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post your current code, and I'll take a look at it. You're right, that this should be straight forward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-02-2011, 08:19 PM
  2. Sentence count
    By mrkrinkle in forum C Programming
    Replies: 4
    Last Post: 02-10-2010, 11:32 AM
  3. Replies: 5
    Last Post: 09-28-2004, 12:38 PM
  4. How to count the words in sentence ?
    By Th3-SeA in forum C Programming
    Replies: 1
    Last Post: 10-01-2003, 01:34 AM
  5. Count the number of letter "a" on a sentence
    By imbecile in C in forum C Programming
    Replies: 6
    Last Post: 07-27-2003, 02:32 PM

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