venom
venom

Reputation: 15

How to truncate a string array? C++

Let's say I have a string array with 5 words and I want to only output the first 3 letters of each word. How do I go upon doing this? I know how to do it with one string but with an array of strings I get lost.

This is how to do it with one string

std::string test = "hello";

std::cout << test << std::endl;

test = test.substr(0,3);

std::cout << test << std::endl;

What I want to do is this

std::string test[5] = {"hello", "pumpkin", "friday", "snowboard", "snacks"};

I want to cout the first 3 letters of each word. I tried test[5] = test[5].substr(0,3); and that did not work.

Upvotes: 0

Views: 2376

Answers (5)

Felix Glas
Felix Glas

Reputation: 15524

Use the standard library.

std::for_each(std::begin(test), std::end(test), [] (auto& s) { s.erase(3); });

Or even a simple range-based for loop:

for (auto&& s : test) {
    s.erase(3); // Erase from index 3 to end of string.
}

Or maybe even create another container with views of the original strings:

auto test2 = std::accumulate(std::begin(test), std::end(test),
                             std::vector<std::string_view>{},
                             [] (auto& prev, std::string_view sv) -> decltype(prev)& {
    prev.push_back(sv.substr(0, 3));
    return prev;
});

Upvotes: 0

Ron
Ron

Reputation: 15501

With test[5] you are reading out of bounds thus invoking undefined behavior. Arrays in C++ are zero indexed so the last element would be test[4]. Create a function that utilizes for example the std::next function or string's substr member function. Call inside a range based loop:

#include <iostream>
#include <string>
void foo(const std::string& s) {
    if (s.size() >= 3) {
        std::cout << std::string(s.begin(), std::next(s.begin(), 3)) << '\n';
        // or simply:
        std::cout << s.substr(0, 3) << '\n';
    }
}
int main() {
    std::string test[5] = { "hello", "pumpkin", "friday", "snowboard", "snacks" };
    for (const auto& el : test) {
        foo(el);
    }
}

Upvotes: 3

schorsch312
schorsch312

Reputation: 5698

substr is what you are looking for. Here is my implementation.

#include <array>
#include <string>
#include <iostream>
int main () {
    std::array<std::string,5> list {"hello", "pumpkin", "friday", "snowboard", "snacks"};

    for (const auto &word : list){
        std::cout << word << std::endl;
    }

    for (auto &word : list){
        word = word.substr(0,3);
    }

    for (const auto &word : list){
        std::cout << word << std::endl;
    }
}

Upvotes: 0

Achal
Achal

Reputation: 11921

 test[5] = test[5].substr(0,3); won't work  and more over you don't have `test[5]`, index starts from `0`.

you may want to do like this

for(int i=0 ; i<5; i++) {
                test[i] = test[i].substr(0,3);
                cout << test[i] << endl;
        }

Upvotes: 0

john
john

Reputation: 87959

test[5] doesn't work because you only have 5 items in your array, only indexes 0 to 4 are valid.

Generally with arrays you need to write a loop to go through each array item in turn, for instance

for (int i = 0; i < 5; ++i)
    test[i] = test[i].substr(0,3);

for (int i = 0; i < 5; ++i)
    cout << test[i] << endl;

Upvotes: 4

Related Questions