String_manipulation didactic material 2 1 variant

  • docx
  • 02.05.2020
Публикация на сайте для учителей

Публикация педагогических разработок

Бесплатное участие. Свидетельство автора сразу.
Мгновенные 10 документов в портфолио.

Иконка файла материала String_manipulation didactic material 2 1 variant.docx

Practical work

Write the following exercises, each in a separate file. Filenames should be:

  • textline.cpp
  • piglatin.cpp

 

1)     Write the following functions. Each of these functions should have a single parameter -- accepting a c-style string as an argument. The function should only do what is specified (note that none of these functions do any output to the screen). Your functions should use const in the prototype wherever it is appropriate:

  1. Write a function that counts and returns the number of vowels in the string. (For the purposes of this exercise, we are talking about the standard 5 vowels -- A, E, I, O, U).
  2. Write a function that counts and returns the number of consonants in the string.
  3. Write a function that converts the string to all lowercase.
  4. Write a function that converts the string to all uppercase.

 

Sample Run

(user input is underlined, to distinguish it from output)

Input a line of text, up to 100 characters:
> The quick brown fox jumped. The lazy dog, he was jumped over.
 
A)  Count the number of vowels in the string
B)  Count the number of consonants in the string
C)  Convert the string to uppercase
D)  Convert the string to lowercase
E)  Display the current string
F)  Enter another string
 
M)  Display this menu
X)  Exit the program
 
Enter your menu selection: a
Number of vowels: 16
 
Enter your menu selection: B
Number of consonants: 31
 
Enter your menu selection: c
 
Enter your menu selection: e
The string:
THE QUICK BROWN FOX JUMPED. THE LAZY DOG, HE WAS JUMPED OVER.
 
Enter your menu selection: D
 
Enter your menu selection: E
The string:
the quick brown fox jumped. the lazy dog, he was jumped over.
 
Enter your menu selection: f
Input a new line of text, up to 100 characters:
> Mary Had A Little Lamb. His name was Fleecy Pete.
 
Enter your menu selection: C
 
Enter your menu selection: e
The string:
MARY HAD A LITTLE LAMB. HIS NAME WAS FLEECY PETE.
 
Enter your menu selection: d
 
Enter your menu selection: E
The string:
mary had a little lamb. his name was fleecy pete.
 
Enter your menu selection: b
Number of consonants: 24
 
Enter your menu selection: a
Number of vowels: 14
 
Enter your menu selection: x
Goodbye

 

2)     Write a function, called ToPigLatin, which is described below:

  • The function prototype, which is:

·           char* ToPigLatin(char* word);

This function receives a c-style string (word) as a parameter.

  • You are to define this function so that it converts the incoming string (character array) to its "Pig Latin" version, and then have the function return a pointer to the string
  • For our purposes, we will use the following as the rules for translation of a word into "Pig Latin":
    1. word is a consecutive sequence of letters (a-z, A-Z) or apostrophes. You may assume that the input to the function will only be a single "word". Examples: Zebra , doesn't , apple
    2. If a word starts with a vowel, the Pig Latin version is the original word with "way" added to the end
    3. If a word starts with a consonant, or a series of consecutive consonants, the Pig Latin version transfers all consonants up to the first vowel to the end of the word, and adds "ay" to the end.
    4. The letter 'y' should be treated as a consonant if it is the first letter of a word, but treated as a vowel otherwise.
    5. If the original word is capitalized, the new Pig Latin version of the word should be capitalized in the first letter (i.e. the previous capital letter may not be capitalized any more).
  • In the starter file is a main() program that I have already provided you with for testing. This program prompts the user to type in 5 words, then it calls the function for each of them and prints the converted version of each of the 5 strings. Note that the main() function does all of the output -- your function should do NO output (just the appropriate conversion and return). The output from your test runs should match mine exactly. Do not change the main() program in any way.
  • You may assume that a "word" to be entered into the function is no more than 39 characters.

 

Sample Runs

(user input is underlined, to distinguish it from output)

Sample Run 1

Input 5 words: Flower yellow bypass apple Igloo
 
Pig Latin version of the 5 words:
Owerflay ellowyay ypassbay appleway Iglooway

Sample Run 2

Input 5 words: string Hamburger Rhythm queen zippitydoodah
 
Pig Latin version of the 5 words:
ingstray Amburgerhay Ythmrhay ueenqay ippitydoodahzay

 

Requirements for both programs

  • No global variables, except where specified
  • You may use any of these libaries:
    • iostream
    • iomanip
    • cctype
    • cstring
    • cstdlib
  • Readable and well-documented source code