If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Class II of Char Arrays & Strings, let's explore more about working with character arrays and strings in C++.
Concatenating Character Arrays: To concatenate two character arrays, you can use the
strcat()
function from the C Standard Library. This function appends the contents of the source array to the destination array, effectively concatenating them. Note that the destination array must have enough space to accommodate the concatenated result.cppCopy code
#include <iostream> #include <cstring> // Include this header for C-string functions int main() { char str1[20] = "Hello"; char str2[] = " World!"; char result[30]; // Ensure enough space to hold the concatenated string strcpy(result, str1); // Copy the first string to the result array strcat(result, str2); // Concatenate the second string to the result array std::cout << "Concatenated string: " << result << std::endl; return 0; }
Comparing Character Arrays: To compare two character arrays, you can use the
strcmp()
function from the C Standard Library. This function compares the contents of two strings and returns an integer value as the result. It returns 0 if the strings are equal, a negative value if the first string is lexicographically less than the second string, and a positive value if the first string is lexicographically greater than the second string.cppCopy code
#include <iostream> #include <cstring> // Include this header for C-string functions int main() { char str1[] = "apple"; char str2[] = "orange"; int result = strcmp(str1, str2); if (result == 0) { std::cout << "Strings are equal.\n"; } else if (result < 0) { std::cout << "str1 is lexicographically less than str2.\n"; } else { std::cout << "str1 is lexicographically greater than str2.\n"; } return 0; }
Working with C++ Strings: The C++ string class provides several useful functions for working with strings. Here are a few commonly used functions:
cppCopy code
#include <iostream> #include <string> // Include this header for C++ string int main() { std::string message = "Hello, World!"; // Length of the string std::cout << "Length of the string: " << message.length() << std::endl; // Accessing individual characters char firstChar = message[0]; char lastChar = message.back(); std::cout << "First character: " << firstChar << std::endl; std::cout << "Last character: " << lastChar << std::endl; // Substring std::string substring = message.substr(0, 5); // Get the first 5 characters std::cout << "Substring: " << substring << std::endl; // Find substring within the string size_t pos = message.find("World"); if (pos != std::string::npos) { std::cout << "Substring found at position: " << pos << std::endl; } else { std::cout << "Substring not found.\n"; } return 0; }
These are just some of the operations you can perform with character arrays and C++ strings. C++ strings provide a wide range of powerful and convenient functions for string manipulation. You can explore the C++ string reference for more details on these functions: https://en.cppreference.com/w/cpp/string/basic_string
I bought this course, it worth it!
Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it
Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website
Quick answers to common questions about our courses, quizzes, and learning platform
SCIAKU Team please upload 1st video of TREE please please please, please