Can anyone help me with my assignment?

0 Mathew Oakley · October 23, 2015
Hi guys

I'm fairly new to programming and can appreciate the fact this is probably an easy program to write but I honestly don't have a clue where to start. Any input is much appreciated.

I have been asked to write code that will create the following structure when a sentence has been entered.

T - upper case
h
e - lower case vowel
 
c
A - upper case vowel
t
, - punctuation
 
s
a
a -  lower case vowel duplicate character
t
. - punctuation


I also need to write the code to determine whether each character is upper case, lower case, a vowel or punctuation. 

Please help. Thanks in advance!

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · October 24, 2015
I recommend using the ctype library for your project. It can handle character identification for seeing if something is a letter, uppercase or punctuation.

I don't really get your description, but what I wrote applies to what I did understand. 
+1 Mathew Oakley · October 26, 2015
I will look into that thank you for your response.

And ye it is a bit confusing. Basically, they want it so when you type something in (using scanf I'm guessing) it returns each character on a new line with a label of what it is. 
0 Tom B · October 26, 2015
Is the first lower case 'a' a typo? The output doesn't really fit with the rest of the program as far as I can tell.
Also are there any requirements as to how to sentence must be entered?

Anyways I made something that works if that is a typo. I did make this quickly. This code breaks some conventions and there are lots of ways to do this better to do this but I tried making it using more basic C/programming concepts so it's easier to focus on the logic and how I broke the problem down.



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

//Vowel count used to check for duplicates.
int a_count = 0, e_count = 0, i_count = 0, o_count = 0, u_count, y_count = 0;

//Check if a character is a vowel and if so update its vowel count .
int isvowel(char chr)
{
    switch(tolower(chr))
    {
        case 'a':
            a_count++;
            return 1;            
            break;
        case 'e':            
            e_count++;
            return 1;
            break;
        case 'i':
            i_count++;
            return 1;
            break;
        case 'o':
            o_count++;
            return 1;
            break;
        case 'u':
            u_count++;
            return 1;
            break;
        case 'y':
            y_count++;
            return 1;
            break;
        default:
            return 0;
    }
}


//Return (vowel)_count to check if the vowel has already been used.
int isduplicate(char chr)
{
    switch(tolower(chr))
    {
        case 'a':
            return a_count;            
            break;
        case 'e':            
            return e_count;
            break;
        case 'i':
            return i_count;
            break;
        case 'o':
            return o_count;
            break;
        case 'u':
            return u_count;
            break;
        case 'y':
            return y_count;
            break;
        default:
            return 0;
    }
}

//The sentence must be entered through the command line parameters
int main(int argc, char *argv[])
{
    //Check if the the user has entered command line parameters.
    if(argv[1] != NULL)
    {
        //String iterator.
        int i;
        //Flag to test if the first character has already been passed.
        int first_char_passed = 0;

        //Loop through the string array.
        for(i = 1; i < argc; i++)
        {
            //Character iterator.
            int y;        
        
            //Loop through each string until we get to
            for(y = 0; y <= strlen(argv); y ++)
            {            
                char current_char = argv[y];

                //Check if the first character is upper case regardless it's a vowel.
                //If it is  print it out and set the first_char_passed flag to 1 (true)
                //so that it no longer checks.
                if(isupper(argv[1][0]) && !first_char_passed)
                {
                    printf("%c -upper case\n", current_char);
                    first_char_passed = 1;
                }
                //Check if the current character is upper case and a vowel.
                else if(isupper(current_char) && isvowel(current_char))
                {    
                    //Check if the the vowel count is greater than zero by subtracting
                    //the 1 added by the vowel check and checking if it still isn't 0.
                    if(isduplicate(current_char) - 1)
                        printf("%c -upper case vowel duplicate\n", current_char);
                    else
                        printf("%c -upper case vowel\n", current_char);
                }
                //Check if the current character is lower case and a vowel.
                else if(islower(current_char) && isvowel(current_char))
                {
                    //Check if the the vowel count is greater than zero by subtracting
                    //the 1 added by the vowel check and checking if it still isn't 0.
                    if(isduplicate(current_char) - 1)
                        printf("%c -lower case vowel duplicate\n", current_char);
                    else
                        printf("%c -lower case vowel\n", current_char);
                }
                //Check if the current character is a punctuation mark.
                else if(ispunct(current_char))
                {
                    printf("%c -punctuation\n", current_char);
                }
                //If all checks fail just print out the current character
                else
                {
                    printf("%c\n", current_char);
                }
            }
        }
    }
    else
    {
        fprintf(stderr, "The sentence must be entered through command line parameters.\n");
        return 1;
    }
    return 0;
}
           



I recommend rewriting it because both it's badly made and if it's your assignment it's good to be honest; it'll benefit you in the long run.

This is the output on a Linux box:


tom@TomDesktop:~/Desktop$ gcc -Wall test.c -o test
tom@TomDesktop:~/Desktop$ ./test The cAt, saat.
T -upper case
h
e -lower case vowel

c
A -upper case vowel
t
, -punctuation

s
a -lower case vowel duplicate
a -lower case vowel duplicate
t
. -punctuation

tom@TomDesktop:~/Desktop$ ./test
The sentence must be entered through command line parameters.
tom@TomDesktop:~/Desktop$ ./test




Good luck with stuff! :P
Hope this helps a little.

EDIT: Code was copied improperly. Fixed.
+1 Mathew Oakley · October 28, 2015
That's great Tom B, thanks for the help! I never thought of using a switch. And don't worry, I will be rewriting it, I've still got a lot to learn! 

Thanks again! :)
0 Tom B · October 28, 2015
Whoops just realized I didn't copy everything so my post made like no sense sorry I've fixed it so it's complete now
0 Mathew Oakley · October 30, 2015
Lol even better Tom, cheers for your time mate it's much appreciated:)
  • 1

C

107,249 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator