Question

roblem description Write the following functions as prototyped below. Do not alter the function signatures. Demonstrate each function using literal strings defined in your client program (e.g., dont prompt the user for test inputs) /// Returns a copy of the string with its first character capitalized //I and the rest lowercased /// Example: capitalize(hELLO WORLD) returns Hello world std::string capitalize(const std::string&str) /// Returns a copy of the string centered in a string of length ·width.. /// Padding is done using the specified fillchar (default is an ASCII space). /// The original string is returned if width is less than or equal to the /// strings length. std:: string center(const std: : string& str, size_t width, char fiuchar- · ); /// Returns a copy of the string left justified in a string of length ·width. /// Padding is done using the specified fillchar (default is an ASCII space) /// The original string is returned if width is less than or equal to /// the strings length. Enmpte:njustenietio worlar, 20, ) returns Hetlo vorla std::string ljust(const std::string& str, size t width, char fillchar // Returns a copy of the string with all the uppercase letters converted /// to lowercase. /// Example: lower(HELLO WORLD) returns hello world std::string lower(const std::string& str) /// Returns a copy of the string with leading characters removed. The /11 chars argument is a string specifying the set of characters to be removed. // If omitted, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its value 1// are stripped. // Example: strip(spacious returns spacious /Example: Istrip(www.example.com,cmowz.) returns example.com std:: string Istrip(const std: : string& str, const std: : string& chars \t\n\v\f\r); “ = /// Returns a copy of the string right-justified in a string of length width. /// Padding is done using the specified fillchar (default is an ASCII space) 1/ The original string is returned if width is less than or equal to the /// strings length.
media%2F965%2F9657b8e7-0712-4c4d-86d7-4f
media%2F120%2F120cd4c8-1a39-4a16-9a69-1c
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
using namespace std;

// Returns a copy of the string with its first character capitalized
// and the rest lowercased.
// Example: capitalized("hELLO wORLD") returns "Hello world"
std::string capitalize(const std::string &str)
{
int x;
// Stores the parameter string str in result
string result = str;
// Converts the parameter str first index position character to upper case
// and stores it in first index position of the result string
result[0] = toupper(str[0]);

// Loops from 1 to end of the parameter string str
for(x = 1; x < str.length(); x++)
// Converts current index position character of parameter string str to lowercase
// and stores it at current index position of result string
result[x] = tolower(str[x]);
// Returns the result string
return result;
}// End of function

// Returns a copy of the string centered in a string of length 'width'
// Padding is done using the specified 'fill char' (default is an ASCII space).
// The original string is returned if 'width' is less than or equal to the string's length
// Example: center("Hello world", 20, '.') becomes ".....Hello world...."
std::string center(const std::string &str, size_t width, char fillchar = ' ')
{
int x, y, z;
// Stores the parameter string str in result
string result = str;
// Calculates require number of fill character
// by subtracting the length of the string from parameter width
int required = width - str.length();

// Checks if calculated required is greater than 0
if(required > 0)
{
// Loops till calculated required number
for(x = 0; x < required; x++)
// Adds space at the end
result += ' ';

// Checks if calculated required is even
if(required % 2 == 0)
{
// Loops till half of the calculated required
for(x = 0; x < required / 2; x++)
// Adds the fill character at the beginning
result[x] = fillchar;

// Loops from previous x index position to end of the string
for(y = x, z = 0; z < str.length(); z++, y++)
// Assigns each character from parameter str to result string
result[y] = str[z];

// Loops till half of the calculated required
for(x = y; x < required / 2; x++)
// Adds the fill character at the end
result[x] = fillchar;
}// End of if condition

// Otherwise odd
else
{
// Loops till half of the calculated required plus one times
for(x = 0; x < (required / 2) + 1; x++)
// Adds the fill character at the beginning
result[x] = fillchar;

// Loops from previous x index position to end of the string
for(y = x, z = 0; z < str.length(); z++, y++)
// Assigns each character from parameter str to result string
result[y] = str[z];

// Loops till half of the calculated required
for(x = y, z = 0; z < required / 2; z++)
// Adds the fill character at the end
result[x++] = fillchar;
}// End of else
}// End of if condition

// Returns the result string
return result;
}// End of function

// Returns a copy of the string left justified in a string of length 'width'.
// Padding is done using the specified 'fill char' (default is an ASCII space).
// The original string is returned if 'width' is less than or equal to the string's length
// Example: ljust("Hello world", 20, '.') becomes "Hello world........."
std::string ljust(const std::string &str, size_t width, char fillchar = ' ')
{
int x, z;
// Stores the parameter string str in result
string result = str;

// Calculates require number of fill character
// by subtracting the length of the string from parameter width
int required = width - str.length();

// Loops till half of the calculated required
for(x = 0; x < required; x++)
// Adds space at the end
result += ' ';

// Loops from the length of the parameter string str to the calculated required
for(x = str.length(), z = 0; z < required; z++)
// Adds the fill character at the end
result[x++] = fillchar;

// Returns the result string
return result;
}// End of function

// Returns a copy of the string with all the uppercase letters converted to lowercase.
// Example: lower("HELLO WORLD") returns "hello world"
std::string lower(const std::string &str)
{
string result = str;

// Loops till end of the parameter string str
for(int x = 0; x < str.length(); x++)
// Converts current index position character of parameter string str to lowercase
// and stores it at current index position of result string
result[x] = tolower(str[x]);
return result;
}// End of function

// Returns a copy of the string with leading characters removed.
// The 'chars' argument is a string specifying the set of characters to be remove.
// If omitted, the 'chars' argument defaults to removing whitespace.
// The 'chars' argument is not a prefix: rather, all combinations of its value are stripped.
// Example: lstrip(" spacious ") returns "spacious "
// Example: lstrip("www.example.com", "cmowz.") returns "example.com"
std::string lstrip(const std::string &str, const std::string &chars = " \t\n\v\f\r")
{

}// End of function

// Returns a copy of the string with right - justified in a string of length 'width'
// Padding is done using the specified 'fill char' (default is an ASCII space).
// The original string is returned if 'width' is less than or equal to the string's length.
// Example: rjust("Hello world", 20, '.') returns ".........Hello world"
std::string rjust(const std::string &str, size_t width, char fillchar = ' ')
{
int x, y, z;
// Stores the parameter string str in result
string result = str;

// Calculates require number of fill character
// by subtracting the length of the string from parameter width
int required = width - str.length();

// Loops till the calculated required
for(x = 0; x < required; x++)
// Adds the space character at the end
result += ' ';

// Loops till the calculated required
for(x = 0; x < required; x++)
// Adds the fill character at the end
result[x] = fillchar;

// Loops from previous x value to end of the string
for(y = x, z = 0; z < str.length(); z++)
// Assigns each character from parameter str to result string
result[y++] = str[z];

// Returns the result string
return result;
}// End of function

// Returns a copy of the string with trailing characters removed.
// The 'chars' argument is a string specified the set of characters to be removed.
// If omitted, the 'chars' argument defaults to removing whitespace.
// The 'chars' argument is not a suffix; rather, all combinations of its value are stripped.
// Example: rstrip(" spacious ") returns " spacious"
// Example: rstrip("mississippi", "ipz") returns "mississ"
std::string rstrip(const std::string &str, const std::string &chars = " \t\n\v\f\r")
{

}// End of function

// Returns a copy of the string with leading and trailing characters removed
// The 'chars' argument is a string specifying the set of characters to be removed.
// If omitted, the 'chars' argument defaults to removing whitespace.
// The 'chars' argument is not a prefix or suffix; rather, all combinations
// of its value are stripped.
// Example: strip(" spacious ") returns "spacious"
// Example: strip("www.example.com", "cmowz") returns "example"
std::string strip(const std::string &str, const std::string &chars = " \t\n\v\f\r")
{

}// End of function

// Returns a copy of the string in title case, where words start with an uppercase character
// and the remaining characters are lowercase.
// Example: title("hELLO wORLD") returns "Hello World"
std::string title(const std::string &str)
{
int x;
// Stores the parameter string str in result
string result = str;
// Converts the parameter str first index position character to upper case
// and stores it in first index position of the result string
result[0] = toupper(str[0]);

// Loops from 1 to end of the parameter string str
for(x = 1; x < str.length(); x++)
// Checks if current character is space
if(str[x] == ' ')
// Converts the current index position character of parameter string str to upper case
// and stores it at current index position of result string
result[x] = toupper(str[x]);
// Otherwise convert it to lower case and store
else
result[x] = tolower(str[x]);

// Returns the result string
return result;
}// End of function

// Reverses the order of the characters in the string "in place" without using a temporary string.
// Example: reverse("ABCDE") returns "EDCBA"
void reverse(const std::string &str)
{
// Loops from end of the string minus one position to 0 index position
for(int x = str.length()-1; x >= 0; x--)
// Displays current index position character
cout<<str[x];
}// End of function

// Returns true if the string contains a sequence of digit characters such that the final
// digit can be calculated from the preceding digits.
// 1. Add the digits in the even - numbered positions (zeroth, second, fourth, et.)
// together and multiply by three.
// 2. Add the digits (up to but not including the final digit) in the odd - numbered
// positions (first, third, fifth, etc.) to the result.
// 3. Take the remainder of the result divided by 10, and if it's not zero, subtract this
// from 10 to determine the last digit.
// 4. If the calculated digit is the same as the string's last digit, the string is
// formed properly, and the function will return true.
//
// The function returns false:
// a. If the string's length is not exactly 12,
// b. If the string contains any non - digit characters, or
// c. If the number does not meet the specification.
// Example: The function returns true for "036000241457" or "786936244250",
// but false for "036000291458" or "12hello".
bool secret(const std::string &str)
{
// To store even and odd index position sum
int evenSum = 0, oddSum = 0;
// Extracts last digit from the string and converts it to integer
int lastDigit = str[str.length()-1] - '0';

// Checks the length of the string if it is not 12 return false
if(str.length() != 12)
return false;

// Otherwise length of the string is exactly 12
else
{
// Loops till end of the parameter string str
for(int x = 0; x < str.length(); x++)
// Checks if current index position character is not digit return false
if(!isdigit(str[x]))
return false;
}// End of else


// Loops till end of the parameter string str minus one times
for(int x = 0; x < str.length()-1; x++)
// Checks if even index position
if(x % 2 == 0)
// Converts the current index position character to integer
// and adds it to previous even sum
evenSum += str[x] - '0';

// Otherwise odd index position
else
// Converts the current index position character to integer
// and adds it to previous odd sum
oddSum += str[x] - '0';

// Multiplies 3 with even sum
evenSum *= 3;

// Calculate remainder of (even sum + odd sum ) by 10
int rem = (evenSum + oddSum) % 10;

// Checks if remainder is not 0
if(rem != 0)
// Subtract remainder from 10
rem = 10 - rem;

// Check if remainder is equals to last digit of string return true
if(rem == lastDigit)
return true;
// Otherwise return false
else
return false;
}// End of function

// main function definition
int main()
{
string str = "hELLO wORLD";
cout<<"\n First letter capitalized rest lowercase: "<<capitalize(str);
cout<<"\n Title case: "<<title(str);
cout<<"\n Lower case: "<<lower(str);

str = "Hello world";
cout<<"\n\n Center fill with (.): "<<center(str, 20, '.');
cout<<"\n Left Justified fill with (.): "<<ljust(str, 20, '.');
cout<<"\n Right Justified fill will (.): "<<rjust(str, 20, '.');

cout<<"\n\n Reverse (This is): ";
reverse("This is");

str = "036000241457";
if(secret(str))
cout<<"\n Secret number \"036000241457\": TRUE";
else
cout<<"\n Secret number \"036000241457\": FALSE";

str = "786936244250";
if(secret(str))
cout<<"\n Secret number \"786936244250\": TRUE";
else
cout<<"\n Secret number \"786936244250\": FALSE";

str = "036000291458";
if(secret(str))
cout<<"\n Secret number \"036000291458\": TRUE";
else
cout<<"\n Secret number \"036000291458\": FALSE";

str = "123hello";
if(secret(str))
cout<<"\n Secret number \"123hello\": TRUE";
else
cout<<"\n Secret number \"123hello\": FALSE";
}// End of main function

Sample Output:

First letter capitalized rest lowercase: Hello world
Title case: Hello world
Lower case: hello world

Center fill with (.): .....Hello world....
Left Justified fill with (.): Hello world.........
Right Justified fill will (.): .........Hello world

Reverse (This is): si sihT
Secret number "036000241457": TRUE
Secret number "786936244250": TRUE
Secret number "036000291458": FALSE
Secret number "123hello": FALSE

Add a comment
Know the answer?
Add Answer to:
roblem description Write the following functions as prototyped below. Do not alter the function signatures. Demonstrate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • Write a function called smallestLetter that takes in a string argument and returns a char. The...

    Write a function called smallestLetter that takes in a string argument and returns a char. The char that is returned by this function is the character in the string with the lowest ASCII integer code. For example: smallestLetter("Hello") returns ‘H’ which is code 72, and smallestLetter("Hello World") returns ‘ ’ (The space character) which is code 32

  • You are to write two functions, printString() and testString(), which are called from the main function....

    You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints characters to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called. Some sample runs are given below: string: “hello” printString prints: h e l l o testString returns: true...

  • Write a class called StringCode with the following functions. (Do not change these function names or...

    Write a class called StringCode with the following functions. (Do not change these function names or return types, else tests will fail). public static String blowup (String str) Returns a version of the original string as follows: each digit 0-9 that appears in the original string is replaced by that many occurrences of the character to the right of the digit. So the string "a3tx2z" yields "attttxzzz" and "12x" yields "2xxx". A digit not followed by a character (i.e. at...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Does any one can show me the code in C++ ll cricket LTE 80% 16:58 Back...

    Does any one can show me the code in C++ ll cricket LTE 80% 16:58 Back P2.B MYString v1 Detail Submission Grade For this program, your MYString variables will never need to grow beyond length 20, in program 3 you will need to be allow your string that is held in the class to be able to be larger than 20 chars. So you may want to start allowing your strings to be able to grow....if you have extra time....

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • How do I do this? -> string print() const; Function to output the data, return the...

    How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space this is my code: template <class elemType> string hashT<elemType>::print() const { for (int i = 0; i < HTSize; i++){        if (indexStatusList[i] == 1){        cout <<HTable[i]<< " " << endl;        }   }    } **********Rest of the code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT