Thread: Basic C Programming Help Needed

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    4

    Talking Basic C Programming Help Needed

    Hi all

    I hope that maybe one of you is nice enough to give me a nudge in the right direction with this assignment I am working on. I have been given a program shell that is incomplete and asked to finish it. At the moment I feel completely stuck in never never land and I cant seem to get my head around any of it.
    Some suggestions and help would be most appreciated.

    Here is the assignment if you could please take the time to look over it:
    Thanks
    Smurphygirl
    p.s. dont hate me if i didnt get the code tag right. I did try

    Code:
    #include "copyio.h"
    #include <string.h>
    #include <ctype.h>
    
    //function prototypes
    void outputHeader(void);
    int getText(char input[]);
    int offerMenu(void);
    void implementChoice(int length, int choice, char input[]);
    int countPunct(int length, char input[]);
    int checkIfPalindrome(int length, char input[]);
    void printAsciiBinary(int length, char input[]);
    
    /******************************************************************************
    main 
    The main function.
    Calls outputHeader, getText, offerMenu, implementChoice.
    ******************************************************************************/
    
    int main (void)
    {	
    	int lengthOfInput;
    	int inputChoice;
    	char inputString[81]; //80 character limit on text input
    	int quit;
    	
    	outputHeader();
    	
    	lengthOfInput = getText(inputString); //prompts for and gets an input string 
    							                //and its length
    	quit = 0;
    	inputChoice = 0;
    	while (!quit) 
    	{												 
    		inputChoice = offerMenu();		
    		implementChoice(lengthOfInput, inputChoice, inputString);
    		if (inputChoice == 5)
    		{
    			quit = 1;
    			printf("Program terminated.\n");
    		}
    	}
    	
    	return (0);
    		
    }//end of main
    
    /******************************************************************************
    outputHeader
    Displays the program heading.
    Called by main.
    ******************************************************************************/
    
    void outputHeader(void)
    {
    
    }//end of outputheader
    
    /******************************************************************************
    getText
    Gets the text from the input stream and stores it in the "input" string.
    Assumes text input is limited to one line of up to 80 characters.
    Called by main.  
    Calculates the length of the string.
    Receives base address of "input" string.
    Returns inputLength.
    ******************************************************************************/
    
    int getText(char input[])	
    {
    	int inputLength;
    		
    	printf("Enter text to be processed (1 line up to 80 characters): \n");
    	printf("> ");
      gets(input);
      inputLength = strlen(input);
       
      return (inputLength);
          
    }//end of getText
    
    /******************************************************************************
    offerMenu
    Displays menu of text processing and quit options.
    Called by main.
    Returns user's choice. 
    ******************************************************************************/
    
    int offerMenu(void)
    {
    	int choice;
    	
    	 printf("\n\nMenu:\n");
       printf("  1:  Count characters (including white spaces)\n");
       printf("  2:  Count punctuation marks\n");
       printf("  3:  Check if palindrome\n");  	
       printf("  4:  Print table of characters in ascii and binary\n");
       printf("  5:  Quit\n");
       	
       printf("\n> ");  	 	
       scanf("%d", &choice);
       
       return (choice);
       
    }//end of offerMenu
    
    /******************************************************************************
    implementChoice
    Implements the user's choice of text processing or quitting program.
    Called by main.
    Calls countPunct, checkIfPalindrome, printAsciiBinary.
    Displays results.
    Receives length of input string, user's choice, base address of input string. 
    ******************************************************************************/
    
    void implementChoice(int length, int choice, char input[])
    {
    	
    }//end of implementChoice
    
     
    /******************************************************************************
    countPunct
    Counts the number of punctuation characters in the input string.
    Called by implementChoice.
    Receives length of input string and base address of input string.
    Returns number of punctuation characters in the input string.
    ******************************************************************************/
    
    int countPunct(int length, char input[])
    {
    	
    }//end of countPunct
    
    /******************************************************************************
    checkIfPalindrome
    Checks if the input string is palindrome.
    Called by implementChoice.  
    Receives length of input string and base address of input string.
    Returns value of isPalindrome.
    ******************************************************************************/
    
    int checkIfPalindrome(int length, char input[])
    {
    			
    }//end of checkIfPalindrome
    
    /******************************************************************************
    printAsciiBinary
    Prints a table of each character in the input string along with its ascii and
    binary values.
    Called by implementChoice.
    Receives length of input string and base address of input string.
    ******************************************************************************/
    
    void printAsciiBinary(int length, char input[])
    {
    		 
    }//end of printAsciiBinary

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Some suggestions and help would be most appreciated.
    Why don't you narrow it down a bit? You seem to have a few empty functions, are you wanting those written? If so, pick one, try yourself, and post here with your problems/questions.

    In other words, try and be more specific!

    And the code tags look good, btw.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    1) Not very difficult. Just start at inputString[0] and start counting until you've reached '\0' (NULL) which indicates the end of the string.

    2) Once again you start from inputString[0]. Check each character and compare them with all possible punctuations. Increment your count if it's a punctuation. Otherwise just continue on until you've hit '\0'

    3) This is a little bit more tricky but still easy. Start comparing from inputString[0] and inputString[lastCharacterPosition]. If they match then check the next one: inputString[1] and inputString[lastCharacterPosition-1]...It's a palindrome if you can scan all the way to the middle. It's a tad easier if you scan all the way through but that's your choice.

    4) To print a character's ascii value, you just treat it as an int and then print that value. For the binary value it's a bit more tricky. Check from bit position 7 all the way down to 0. Printing 1's and 0's along the way depending on what the value is.

    5) You've already got this part. Also you might want to print that quit message outside of the while loop. Just a matter of preference.

    Good luck
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    4

    Hmm

    Thank you peoples for your help. Sadly you might have to word your replies in plainer english for me. This is my first programming paper and my skills arent that good.
    I have been working on this part:
    Code:
    /******************************************************************************
    countPunct
    Counts the number of punctuation characters in the input string.
    Called by implementChoice.
    Receives length of input string and base address of input string.
    Returns number of punctuation characters in the input string.
    ******************************************************************************/
    
    int countPunct(int length, char input[])
    {
    	if (ispunct(input))
    		printf("%d punctuation character(s) in: %s\n", count,input);
    	
    }//end of countPunct
    but somehow I dont think I am doing it right. I am not sure how to get it to count the punctuated characters.

    Thanks
    Smurphy

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >I am not sure how to get it to count the punctuated characters
    Do you understand arrays? If so, you need to loop through the input[] array, checking each character with ispunct(), keeping a count as you go. Remember there are "length" characters in the array.

    Then you printf() after looping through the array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Okay here's an example with commas only:
    Code:
    int punctuationCount = 0;
    while(input[i] != '\0')
    {
       if(input[i] == ',')
          ++punctuationCount;
       ++i;
    }
    return punctuationCount;
    See that wasn't so bad was it? Now you have to work on getting it to handle all punctuations.

    EDIT - You guys think this seems a bit hard for a 1st assignment?
    Last edited by Cshot; 09-26-2002 at 06:01 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int x;
    for( x = 0; x < length; x++)
        if( ispunct( input[x] )
    Replace your 'if' line with that.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    4

    Me again

    Hey peoples
    Yep it would help if I knew about arrays. Which is what I am trying to learn right now. Your examples all look good and seem easy but I think I am still screwing it up.

    I am trying to learn bits and pieces out of the book so I can follow what you are talking about.
    hehe I definitely agree that this is hard for a first assignment.

    Thanks
    Smurphy

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    4

    Ascii and Binary

    Righties I have decided to move on to something else to see if I can do that at all.
    Part 4 of the menu is Print a table of characters in ascii and binary.
    I am clueless about this. As you can probably tell since I am so useless at the other stuff....usually it clicks but today it just isnt.

    Thanks
    Smurphy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Basic Help Needed
    By pobri19 in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2008, 04:57 AM
  2. Basic C-- Help Desperately Needed
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 02-14-2008, 11:08 PM
  3. Need a basic Linux web page designer (editor)
    By BobS0327 in forum Tech Board
    Replies: 5
    Last Post: 12-30-2006, 05:30 PM
  4. VB vs. BASIC
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-05-2002, 08:55 PM
  5. DJGPP help needed
    By dune911 in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2001, 04:56 PM