answersLogoWhite

0


Best Answer

#include<iostream> #include<string> #include<sstream> #include<vector> #include<conio.h> // required by getch() std::string input_string (std::string prompt) { while (true) { std::string input; std::cout << prompt; std::getline (std::cin, input); if (input.size()) return input; std::cout << "Invalid input. Empty strings are not permitted.\n"; } } size_t input_unsigned (std::string prompt) { while (true) { std::string input = input_string (prompt); std::stringstream ss; ss << input; size_t s; if (ss>>s && s) return s; std::cout << "Invalid input. Values must be non-zero.\n"; } } void string_length() { std::string str = input_string ("Enter the string you wish to know the length of:\n"); std::cout << "The string is " << str.size() << " characters long.\n" << std::endl; } void string_compare() { std::pair<std::string, std::string> strings; strings.first = input_string ("Enter the first string you wish to compare:\n"); strings.second = input_string ("Enter the second string you wish to compare:\n"); int compare = strings.first.compare (strings.second); std::cout << "The result of the camprison is: " << compare << '\n' << std::endl; } void string_reverse() { std::string str = input_string ("Enter the string you wish to reverse:\n"); for (size_t f=0, r=str.size()-1; f<r; ++f, --r) { char t = str[f]; str[f] = str[r]; str[r] = t; } std::cout << "The reversed string is:\n" << str << '\n' << std::endl; } void string_substring() { std::string str = input_string ("Enter a string:\n"); size_t pos = input_unsigned ("Enter the offset of the substring:\n"); size_t len = input_unsigned ("Enter the length of the substring:\n"); std::cout << "The substring is: " << str.substr(pos,len) << '\n' << std::endl; } void string_concatenation() { std::pair<std::string, std::string> strings; strings.first = input_string ("Enter the first string you wish to concatenate:\n"); strings.second = input_string ("Enter the second string you wish to concatenate:\n"); std::string concat = strings.first + strings.second; std::cout << "The result of the concatenation is: " << concat << '\n' << std::endl; } int main() { while (true) { std::cout << "MAIN MENU\n"; std::cout << "=========\n" << std::endl; std::cout << "1 - String length\n"; std::cout << "2 - String compare\n"; std::cout << "3 - String reverse\n"; std::cout << "4 - String substring\n"; std::cout << "5 - String concatenation\n"; std::cout << "X - Exit program\n"; #pragma warning(disable : 4996) switch ((char) getch()) { case ('1'): string_length(); break; case ('2'): string_compare(); break; case ('3'): string_reverse(); break; case ('4'): string_substring(); break; case ('5'): string_concatenation(); break; case ('x'): case ('X'): return 0; } } }

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a menu based program in functions to calculate string length string compare string reverse string substring string concatenation?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How to perform string operations like concatenation compare etc without using built in functions?

performing string operation using pointers


What is PHP operators?

PHP Operators are syntactical constructs that assign, compare, or modify a value. There are bitwise operators, arithmetic operators, boolean operators, assignment operators, and concatenation operators. There are also a wide variety of functions and class methods which simulate or utilize these operations.


What does it mean to compare the density of an object to that of it in pieces?

It means: * Calculate the density of an object * Calculate the density of its pieces * Compare


Is 3.50 closer to 3 or 4?

Calculate the difference, and compare. That is, calculate how much is 4 - 3.5, and 3.5 - 3.


What are the functions of an iPod MP3 player?

http://www.apple.com/ipod/compare-ipod-models/


How do you find whether a cone and a cylinder have equal surface area?

Calculate them and compare.


Compare the functions of a human embryonic membranes with those inside a bird's egg?

UM ugh....


How do you calculate the size of Air Purifier you need for your house?

you need to calculate the square footage, and then the area, and compare to the area of air cleaned by a purifier.


Is Facebook better than Skype?

They have different functions and fulfil different requirements. It is not possible to compare them directly.


How do arithmetic and geometric sequences compare to continuous functions?

an arithmetic sequeunce does not have the sum to infinty, and a geometric sequence has.


Compare the functions of the stratum corneum of a tree?

The stratum corneum is the toughest, outermost layer of the epidermis. If I were to compare it to a tree component, I'd say the stratum corneum resembles bark.


How do i compare and contrast adioh and inventory turns calculate the adioh of cost of goods?

adioh calculation