In this part of my C++ tutorial we’ll cover numerous ways to interact with Strings, numerous common Math functions and we’ll solve a rather complex 2 part problem. The goal of this series is to teach the syntax of the C++ language while actually teaching how to solve problems using C++. Each tutorial includes increasingly more complex problems for you to solve. All of the code and a transcript follows the video below.
If you like videos like this consider donating $1, or simply turn off AdBlocker. Either helps me to continue making these free educational videos.
Cheat Sheet / Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
// ----- STRING TUTORIAL ----- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <numeric> #include <sstream> int main() { // A C character string is an array of characters // with a null character at the end \0 char cString[] = {'A', ' ', 'S', 't', 'r', 'i', 'n', 'g', '\0'}; std::cout << cString << "\n"; // Get array size (null is included) std::cout << "Array Size " << sizeof(cString) << "\n"; // C strings are troublesome because if you forget \0, or // add to much information it can lead your program to crash, // or for your system to crash // You can create a vector of strings std::vector<std::string> strVec(10); // C++ std::string can grow in size and is much safer std::string str("I'm a string"); strVec[0] = str; // You can access characters with an index std::cout << str[0] << "\n"; // You can also use at() std::cout << str.at(0) << "\n"; // Front returns first char and back returns last std::cout << str.front() << " " << str.back() << "\n"; // Get the string length std::cout << "Length : " << str.length() << "\n"; // You can copy a string to another std::string str2(str); strVec[1] = str2; // You can copy after the 1st 4 characters std::string str3(str, 4); strVec[2] = str3; // Repeat a value to make a string std::string str4(5, 'x'); strVec[3] = str4; // Combine strings with append or + strVec[4] = str.append(" and your not"); str += " and your not"; // Append part of a string str.append(str, 34, 37); strVec[5] = str; // Erase characters from a string from an index to another // or the last str.erase(13, str.length() - 1); strVec[6] = str; for(auto y: strVec) std::cout << y << "\n"; // find() returns index where pattern is found // or npos if(str.find("string") != std::string::npos) std::cout << "1st not " << str.find("string") << "\n"; // substr(x, y) returns a substring starting at // index x with a length of y std::cout << "Substr " << str.substr(6,6) << "\n"; // Reverse a string by passing the beginning and end // of a string reverse (str.begin(), str.end()); std::cout << "Reverse " << str << "\n"; // Case conversion transform(str2.begin(), str2.end(), str2.begin(), ::toupper); std::cout << "Upper " << str2 << "\n"; transform(str2.begin(), str2.end(), str2.begin(), ::tolower); std::cout << "Lower " << str2 << "\n"; // You can get the ascii code for a char by saving // the char as an int or with (int) // a - z : 97 - 122 // A - Z : 65 - 90 char aChar = 'Z'; int aInt = aChar; std::cout << "A Code " << (int)'a' << "\n"; // Convert int to string std::string strNum = std::to_string(1+2); std::cout << "String " << strNum << "\n"; // ----- PROBLEM : SECRET STRING ----- // Receive an uppercase string and hide its meaning // by turning it into ascii codes // Translate it back to the original letters std::string normalStr, secretStr = ""; std::cout << "Enter your string in uppercase : "; std::cin >> normalStr; // Cycle through each character converting // them into ascii codes which are stored in // a string for(char c: normalStr) secretStr += std::to_string((int)c); // secretStr += std::to_string((int)c - 23); std::cout << "Secret : " << secretStr << "\n"; normalStr = ""; // Cycle through numbers in string 2 at a time for(int i = 0; i < secretStr.length(); i += 2){ // Get the 2 digit ascii code std::string sCharCode = ""; sCharCode += secretStr[i]; sCharCode += secretStr[i+1]; // Convert the string into int int nCharCode = std::stoi(sCharCode); // Convert the int into a char char chCharCode = nCharCode; // char chCharCode = nCharCode + 23; // Store the char in normalStr normalStr += chCharCode; } std::cout << "Original : " << normalStr << "\n"; // ----- END OF PROBLEM : SECRET STRING ----- // ----- BONUS PROBLEM ----- // Allow the user to enter upper and lowercase // letters by subtracting and adding 1 value // ----- END OF BONUS PROBLEM ----- return 0; } // ----- END OF STRING TUTORIAL —— // ----- MATH FUNCTIONS ----- // C++ has numerous math functions // http://en.cppreference.com/w/cpp/numeric/math // Needed for math functions #include <cmath> int main() { std::cout << "abs(-10) = " << std::abs(-10) << "\n"; std::cout << "max(5,4) = " << std::max(5,4) << "\n"; std::cout << "min(5,4) = " << std::min(5,4) << "\n"; std::cout << "fmax(5.3,4.3) = " << std::fmax(5.3,4.3) << "\n"; std::cout << "fmin(5.3,4.3) = " << std::fmin(5.3,4.3) << "\n"; // e ^ x std::cout << "exp(1) = " << std::exp(1) << "\n"; // 2 ^ x std::cout << "exp2(1) = " << std::exp2(1) << "\n"; // e * e * e ~= 20 so log(20.079) ~= 3 std::cout << "log(20.079) = " << std::log(20.079) << "\n"; // 10 * 10 * 10 = 1000, so log10(1000) = 3 std::cout << "log10(1000) = " << std::log10(1000) << "\n"; // 2 * 2 * 2 = 8 std::cout << "log2(8) = " << std::log2(8) << "\n"; // 2 ^ 3 std::cout << "pow(2,3) = " << std::pow(2,3) << "\n"; // Returns what times itself equals the provided value std::cout << "sqrt(100) = " << std::sqrt(100) << "\n"; // What cubed equals the provided std::cout << "cbrt(1000) = " << std::cbrt(1000) << "\n"; // Hypotenuse : SQRT(A^2 + B^2) std::cout << "hypot(2,3) = " << std::hypot(2,3) << "\n"; std::cout << "ceil(10.45) = " << std::ceil(10.45) << "\n"; std::cout << "floor(10.45) = " << std::floor(10.45) << "\n"; std::cout << "round(10.45) = " << std::round(10.45) << "\n"; // Also sin, cos, tan, asin, acos, atan, atan2, // sinh, cosh, tanh, asinh, acosh, atanh return 0; } // ----- END OF MATH FUNCTIONS ----- |
Leave a Reply