1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with C Programming...Explanatin

Discussion in 'Code Snippets and Tutorials' started by rarehunternick, May 2, 2009.

Thread Status:
Not open for further replies.
  1. rarehunternick

    rarehunternick Level II

    Joined:
    Mar 18, 2007
    Messages:
    327
    Likes Received:
    0
    Ok, so i have to write 2 codes base on the following questions:

    1) Write a program that takes nouns and forms their plurals on the basis of these rules:
    a. If the noun ends in "y," remove the "y" and add "ies."
    b. If the noun ends in "s," "ch," or "sh," add "es"
    c. In all other cases, just add "s."

    So, i know i have to use if/else statement and strlen to get what I want to do. But I don't understand how to initially set it up. Ok, maybe I don't know what it looks like. So, maybe someone can give me an example of the string manipulation? I really hate strings.

    2) Define a structure type elemnt_t to represent one element from the periodic table of elements. Compounds should include the atomic number(integer); the name, chemical symbol, and class (strings); a numeric field for the atomic weight; and seven-element array of integers for the number of electrons in each shell. The following are the components of an element_t structure for sodium.

    11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0

    Define and test I/O functions scan_element and print element.

    Ok, i understand this is like an 80 line code, so i know no one will write me the whole code and I do not condemn them to do that. Basically, my question is, how do I get the information in? Thats my only real question. I'm sure I can figure out the code once I understand how to get the information in and then the only thing that I am really confused on is what does it mean to "Define and test I/O functions scan_element and print element?"

    So, any help is appreciated.
     
  2. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    Well it may be a little late but, the code seems fun to write. Here's an example written in C++ and compiled with gcc. (this code should also work with visual studio )

    To compile the code in unix
    1- save the file (in my case I named it Pluriel.cpp)
    2- write this commande line in your folder where the file is (if using unix )
    gcc Pluriel.cpp -lstdc++ -o Pluriel
    3- type in the command line
    ./Pluriel

    Here's the code

    Code (Text):
    1. #include <iostream>
    2. #include <string.h>
    3.  
    4. using namespace std;
    5.  
    6. #define END_WITH_Y 1
    7. #define END_WITH_S_SH_CH 2
    8. #define END_WITH_NOTHING_SPECIAL 3
    9.  
    10.  
    11. int checkKindOfNoun(string tmp)
    12. {
    13.    
    14.     string subStringOneChar = tmp.substr( tmp.length() -1  , 1 );
    15.     if( subStringOneChar == "y" )
    16.     {
    17.         return  END_WITH_Y ;
    18.     }
    19.     string subStringTwoChar =  tmp.substr( tmp.length() -2  , 2 );
    20.      if(subStringTwoChar == "sh" || subStringTwoChar == "ch" || subStringOneChar == "s" )
    21.     {
    22.         return END_WITH_S_SH_CH;
    23.     }
    24.     else
    25.     {
    26.         return END_WITH_NOTHING_SPECIAL;
    27.     }
    28.  
    29. }
    30.  
    31. string addPlurialOne(string tmp)
    32. {
    33.     string subString =tmp.substr( 0  , tmp.length() -1 );
    34.     subString = subString + "ies";
    35.     return subString ;
    36. }
    37.  
    38. string addPlurialTwo(string tmp)
    39. {  
    40.     tmp = tmp +"es";
    41.     return tmp ;
    42. }
    43. string addPlurialThree(string tmp)
    44. {  
    45.     tmp =tmp +  "s";
    46.     return tmp ;
    47. }
    48.  
    49. int main( void )
    50. {
    51.     cout << "Enter your noun, we will changes it to plurial !" << endl;
    52.     string noun = "";
    53.     cin >> noun ;
    54.  
    55.     int typeOfNoun = checkKindOfNoun(noun);
    56.     //cout << typeOfNoun ;
    57.     switch(typeOfNoun)
    58.     {
    59.         case END_WITH_Y :  
    60.                 cout << addPlurialOne(noun)<<endl;
    61.                 break;
    62.         case END_WITH_S_SH_CH :
    63.                 cout << addPlurialTwo(noun)<<endl;
    64.                 break;
    65.         case END_WITH_NOTHING_SPECIAL :
    66.                 cout << addPlurialThree(noun)<<endl;
    67.                 break;
    68.        
    69.     }
    70.    
    71.     return  0;
    72.  
    73. }
     
    Brian likes this.
Thread Status:
Not open for further replies.