Team LiB
Previous Section Next Section

Exercises

  1. Follow the instructions included in Appendix C to get the latest version of Microsoft's .NET Framework SDK running on your computer.

  2. Write a C# program that will print out the integers from 1 to 10 in reverse order.

  3. Write a C# program that will accept a series of individual characters, separated by one or more spaces, as command-line input, and will then "glue" them together to form a word. For example, if we were to invoke the executable Glue.exe as follows:

    Glue B A   N A   N A
    

    then the program should output

    BANANA
    

    with no spaces.

  4. Advanced exercise: Write a C# program that accepts a sentence as command-line input, and outputs statistics about this sentence. For example, if we were to invoke the program as follows:

    SentenceStatistics this is my sample sentence
    

    then the program should output the following results:

    number of words:             5
    longest word(s):             sentence
    length of longest word(s):   8
    shortest word(s):            is my
    length of shortest word(s):  2
    

    (To keep things simple, don't use any punctuation in your sentence.)

    Note?/td>

    Hint: See the section titled "Reading Command-Line Arguments" for ideas on how to approach this.

  5. Advanced Exercise: Write a C# program that accepts strings as command-line arguments, and outputs these strings in sorted order. For example, if we were to invoke the program as follows:

    StringSorter dog cat bird fish
    

    then the program should output the following results:

            bird
            cat
            dog
            fish
    

    In writing this program, use the bubble sort algorithm, which is a simple way to sort a list of items by comparing them in pairwise fashion.

    • Start by comparing the first string to the second and, if the second is less than the first, swap their positions; then, compare the second to the third, etc.

    • Keep making passes through the entire list from beginning to end, comparing them in pairwise fashion, until you are able to get through the list without having to make any "swaps."

    • Then, have your program print out the results.


Team LiB
Previous Section Next Section