0

My asssignmet is:

Input three strings into an array of strings. First, use a function to find the length for the end comparisons, then again compare the last two characters separately. Second, use a string function to take a sub-string of the first three characters for the beginning comparison all at once.

{
    string stringarray[3] = {"yankee", "yes", "word"};
    for (int x = 0; x < 3; x++)
        string substringend1 = stringarray.substr(stringarray.length(stringarray[x]) - 2, stringarray.length(stringarray[x]));
        string substringend2 = stringarray.substr(stringarray.length(stringarray[x]) - 1, stringarray.length(stringarray[x]));
        string substringstart = stringarray.substr(0, 3);

        if (substringend1 == "e" && substringend2 == "s" || substringstart == "yan") {
            for (int y = 0; y < 3; y++)
                cout << stringarray[y];
        }
}

I know Im an idiot and this is a bad question format and whatever but I need help
  • So... print strings in `stringarray` only if they start with `yan` and end with `es`? – silverfox Jun 09 '21 at 03:14
  • Yes but i cant get this to work – Maxilos99999 Jun 09 '21 at 03:17
  • https://stackoverflow.com/questions/1878001/how-do-i-check-if-a-c-stdstring-starts-with-a-certain-string-and-convert-a And C++20 has both `std::string::starts_with()` and `std::string::ends_with()` functions – Chad Jun 09 '21 at 03:38

2 Answers2

0

C++20 makes this very easy:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
int main() {
    std::vector<std::string> in{"yankee", "yes", "word"};
 
    for(const auto& s : in)
    {
        if(s.starts_with("yan") || s.ends_with("es"))
            std::cout << s << '\n';
    }
 
 
    return 0;
}

Live Example: https://godbolt.org/z/6Eq3nnKrf

The hardest part is finding C++20 support in your compiler. Later versions of gcc have it, latest versions of MSVC have it.

Without C++20, you need to find replacements for starts_with() and ends_with(). Starts with is easy and has a good answer here: How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?

Using that answer we can write a pretty simple starts_with() function:

bool starts_with(const std::string& in, const std::string& prefix)
{
    return in.rfind(prefix, 0) == 0;
}

We can use that as an example on how to create an ends_with() function too!

bool ends_with(const std::string& in, const std::string& prefix)
{
    return in.rfind(prefix) == (in.size() - prefix.size());
}

Then our main code changes only slightly:

int main()
{
    std::vector<std::string> in{"yankee", "yes", "word"};
 
    for(const auto& s : in)
    {
        if(starts_with(s, "yan") || ends_with(s, "es"))
            std::cout << s << '\n';
    }
    return 0;
}
Chad
  • 18,706
  • 4
  • 46
  • 63
0

You can use std::compare to compare the start of a string with yan or its end with es:

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

bool startWithString(string a, string mod)
{
    if (a.length() < mod.length()) {return false;}
    return 0 == a.compare(0, mod.length(), mod);
}

bool endWithString(string a, string mod)
{
    if (a.length() < mod.length()) {return false;}
    return  0 == a.compare(a.length()-mod.length(), mod.length(), mod);
}

int main()
{
    string str[4] = {"yankee", "yes", "word", "yankes"};

    for (int i = 0; i < 4; i++)
    {
        string cur = str[i];
        if (startWithString(cur, "yan") || endWithString(cur, "es")) {cout << cur << endl;}
    }
}

Result :

yankee
yes
yankes

Using rfind() also yields similar result:

bool startWithString(string a, string mod)
{
    return a.rfind(mod, 0) == 0;
}

bool endWithString(string a, string mod)
{
    return a.rfind(mod) == (a.length() - mod.length());
}

As @Chad mentioned, C++20 also support starts_with() and ends_with(), which makes stuff easier.

silverfox
  • 1,568
  • 10
  • 27