C++ language
must use strings and c-strings similar to examplesshown above task
C++ language
must use strings and c-strings similar to examplesshown above task
#include #include #include using namespace std; int main () { //getline example for C-Strings char cStr[50]; cout << "Enter a line of input: "; cin.getline(cStr, 50); cout << "cStr = " << cStr << endl; //getline example for the String data type string str; cout << "Enter a line of input: "; getline (cin, str); cout << "str = " << str << endl; getline (cin, str, '?'); cout << "str = " << str << endl; // cin.get example for char input char ch; cout << "Enter a character: " cin.get(ch); cout << "ch = " << ch << endl; //getline returns a reference string s1, s2; cout << "Enter a line of input: "; getline (cin, s1) >> s2; cout << "s1 = " << s1 << endl; cout << "s2 = " << s2 << endl; // Mixing cin and getline int n; string line; cout << "Enter an integer and a string: "; cin >>n; //cin.ignore(50, '\n'); getline(cin, line); cout << "n = " << n << endl; cout << "line = " << line << endl; // Another version of getline // A string can behave like an array string str_arr; cout << "Enter a line of input: "; getline (cin, str_arr); for (int i = } cout << str_arr << " "; 0; i < str_arr.length(); i++) { } cout << endl; for (int i = 0; i < str_arr.length(); i++) { // accessing characters in a string using at() instead of [] cout << str_arr.at(i) << " "; return 0;
Task 2: Order Strings Write a program that takes three strings from user and prints them out in descending order. Sample Interaction: Enter 3 strings: Bob Alice Carol Strings in descending order: Carol Bob Alice
C++ language must use strings and c-strings similar to examples shown above task C++ language must use strings and c-str
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am