C++ Array of Strings

Introduction

In C++, an array of strings is essentially an array of character arrays or an array of std::string objects. Arrays of strings are useful for storing multiple strings in a single structure, making it easier to manage and manipulate collections of strings.

Array of Character Arrays

Defining an Array of Character Arrays

An array of character arrays can be defined using a 2D array where each row is a string.

Example: Defining and Accessing an Array of Character Arrays

#include <iostream>
using namespace std;

int main() {
    // Define an array of character arrays (strings)
    const char* fruits[] = {"Apple", "Banana", "Cherry", "Date"};

    // Get the number of elements in the array
    int numFruits = sizeof(fruits) / sizeof(fruits[0]);

    // Print each string in the array
    for (int i = 0; i < numFruits; i++) {
        cout << fruits[i] << endl;
    }

    return 0;
}

Output

Apple
Banana
Cherry
Date

Explanation

  • const char* fruits[] defines an array of character arrays.
  • sizeof(fruits) / sizeof(fruits[0]) calculates the number of elements in the array.
  • A loop iterates through the array and prints each string.

Array of std::string Objects

Defining an Array of std::string Objects

Using std::string from the C++ Standard Library provides a more convenient and flexible way to handle strings.

Example: Defining and Accessing an Array of std::string Objects

#include <iostream>
#include <string>
using namespace std;

int main() {
    // Define an array of std::string objects
    string fruits[] = {"Apple", "Banana", "Cherry", "Date"};

    // Get the number of elements in the array
    int numFruits = sizeof(fruits) / sizeof(fruits[0]);

    // Print each string in the array
    for (int i = 0; i < numFruits; i++) {
        cout << fruits[i] << endl;
    }

    return 0;
}

Output

Apple
Banana
Cherry
Date

Explanation

  • string fruits[] defines an array of std::string objects.
  • sizeof(fruits) / sizeof(fruits[0]) calculates the number of elements in the array.
  • A loop iterates through the array and prints each string.

Manipulating an Array of Strings

You can manipulate an array of strings by accessing and modifying individual elements.

Example: Modifying an Array of Strings

#include <iostream>
#include <string>
using namespace std;

int main() {
    // Define an array of std::string objects
    string fruits[] = {"Apple", "Banana", "Cherry", "Date"};

    // Get the number of elements in the array
    int numFruits = sizeof(fruits) / sizeof(fruits[0]);

    // Modify an element in the array
    fruits[1] = "Blueberry";

    // Print each string in the array
    for (int i = 0; i < numFruits; i++) {
        cout << fruits[i] << endl;
    }

    return 0;
}

Output

Apple
Blueberry
Cherry
Date

Explanation

  • The second element of the fruits array is modified to "Blueberry".
  • The loop prints the updated array.

Dynamic Array of Strings

Using dynamic memory allocation allows you to create arrays of strings whose size can be determined at runtime.

Example: Dynamic Array of Strings

#include <iostream>
#include <string>
using namespace std;

int main() {
    int numFruits;
    cout << "Enter the number of fruits: ";
    cin >> numFruits;

    // Dynamically allocate an array of std::string objects
    string* fruits = new string[numFruits];

    // Input fruit names
    cout << "Enter the names of the fruits:" << endl;
    for (int i = 0; i < numFruits; i++) {
        cin >> fruits[i];
    }

    // Print each string in the array
    cout << "The fruits you entered are:" << endl;
    for (int i = 0; i < numFruits; i++) {
        cout << fruits[i] << endl;
    }

    // Deallocate the dynamic array
    delete[] fruits;

    return 0;
}

Output

Enter the number of fruits: 3
Enter the names of the fruits:
Apple
Banana
Cherry
The fruits you entered are:
Apple
Banana
Cherry

Explanation

  • The user inputs the number of fruits, and a dynamic array of std::string objects is created.
  • The user inputs the names of the fruits, which are stored in the dynamic array.
  • The array is printed, and then the dynamically allocated memory is deallocated.

Passing an Array of Strings to a Function

You can pass an array of strings to a function to perform operations on it.

Example: Passing an Array of Strings to a Function

#include <iostream>
#include <string>
using namespace std;

// Function to print an array of strings
void printArray(string arr[], int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << endl;
    }
}

int main() {
    // Define an array of std::string objects
    string fruits[] = {"Apple", "Banana", "Cherry", "Date"};

    // Get the number of elements in the array
    int numFruits = sizeof(fruits) / sizeof(fruits[0]);

    // Call the function to print the array
    printArray(fruits, numFruits);

    return 0;
}

Output

Apple
Banana
Cherry
Date

Explanation

  • The printArray function takes an array of std::string objects and its size as parameters.
  • The function iterates through the array and prints each string.

Example Programs

Example 1: Finding the Longest String

#include <iostream>
#include <string>
using namespace std;

string findLongest(string arr[], int size) {
    string longest = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i].length() > longest.length()) {
            longest = arr[i];
        }
    }
    return longest;
}

int main() {
    string fruits[] = {"Apple", "Banana", "Cherry", "Date"};
    int numFruits = sizeof(fruits) / sizeof(fruits[0]);

    string longest = findLongest(fruits, numFruits);
    cout << "The longest fruit name is: " << longest << endl;

    return 0;
}

Output

The longest fruit name is: Banana

Explanation

  • The findLongest function takes an array of std::string objects and its size as parameters.
  • The function iterates through the array to find and return the longest string.

Example 2: Sorting an Array of Strings

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void sortArray(string arr[], int size) {
    sort(arr, arr + size);
}

int main() {
    string fruits[] = {"Apple", "Banana", "Cherry", "Date"};
    int numFruits = sizeof(fruits) / sizeof(fruits[0]);

    sortArray(fruits, numFruits);

    cout << "Sorted fruits:" << endl;
    for (int i = 0; i < numFruits; i++) {
        cout << fruits[i] << endl;
    }

    return 0;
}

Output

Sorted fruits:
Apple
Banana
Cherry
Date

Explanation

  • The sortArray function takes an array of std::string objects and its size as parameters.
  • The function sorts the array using the std::sort function from the C++ Standard Library.
  • The sorted array is then printed.

Conclusion

An array of strings in C++ can be implemented using an array of character arrays or an array of std::string objects. This chapter covered how to define, manipulate, and pass arrays of strings to functions. Example programs demonstrated finding the longest string and sorting an array of strings. Understanding how to work with arrays of strings is essential for handling collections of text data in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top