Char Arrays & Strings - Class II

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 68 :- Char Arrays & Strings - Class II

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

10. Char Arrays and Strings

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?