Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Chapter 3: Loops and Strings: The Pig Latin Program

One of the more useful aspects of computer programs is their capability to repeat tasks over and over. In this chapter you will learn how to make your programs repeat parts of themselves. You will learn a couple types of looping structures and how to decide which one to use for a particular task. Also, you’ll take a closer look at string variables and learn many capabilities of strings in the C# language. Finally, you’ll investigate how to begin designing complex programs. After reading this chapter, you will be able to

Examine an object in the Object Browser.

Use the most common string methods.

Make your program repeat a given number of times.

Make a program that counts backwards and skips numbers.

Write loops that continue an indefinite number of times.

Avoid the most common traps when building loops.

Use the STAIR approach to manage the planning process.

Project: The Pig Latin Program

Your goal this chapter is to write another simple and fun program. This program replicates a silly word game that is very popular with children. The user types in a phrase, and the program calculates the pig latin translation of that phrase. In case you’ve forgotten, pig latin uses a simple formula to make an English word sound like a Latin word: If the word begins with a vowel, you add way at the end of the word. If the word begins with a consonant, you move the consonant to the end of the word and add ay. Look at the program in Figure 3.1 to see an example.

Figure 3.1: The title of this book sounds very classy when translated into pig latin.

The Pig Latin program utilizes a couple features that have not been covered yet in this book, such as text manipulation and repetition. Obviously, the program uses techniques for manipulating text. C# supplies many interesting ways to work with text values, which you will learn about in the next section. Also note that the program repeats in two ways. First, it continues to prompt for a new phrase until the user types in quit. Also, it does the appropriate manipulation to each word in the phrase. Somehow, the program knows how many words are in the phrase. Read on, and you will learn about this and a little more.

55

Соседние файлы в предмете Программирование на C++