3

SEE FINAL UPDATE FOR SOLUTION

This is for school, so I request hints, not blatant answers if possible.

I need to take the phrase "My Name current community college" and output "Current Community College" I already have a loop to capitalize current community college.

Here is basically what I have up to this point:

    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <string>

    using namespace std;

    int main()
    {
        string phrase = "My Name current community college";

        cout << phrase << endl << endl;

        for (int i = 0; i < phrase.length(); i++)
        {
            if (phrase[i] == ' ')
            {
                phrase[i + 1] = toupper(phrase[i + 1]);
            }
        }
    return 0;
    }

I've tried using str.find and str.erase but those seem limited in that they can only search for invididual characters. Is there a way I can search for 'Current' (which would be the name of my community college) and just erase everything before the occurrence of that word in the string?

UPDATE: Thanks to Sam I Am I was able to accomplish my task, here is what I have now:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    string phrase = "My Name current community college";

    cout << phrase << endl << endl;

    for (int i = 0; i < phrase.length(); i++)
    {
        if (phrase[i] == ' ')
        {
            phrase[i + 1] = toupper(phrase[i + 1]);
        }
    }

    size_t pos = phrase.find("Current");
    string output = phrase.substr(pos);

    cout << output << endl << endl;
    system("PAUSE");
    return 0;

}

I'm obsessive though and just getting an A doesn't accomplish my goal. Is there a way to accomplish this task without having to create a new string, as in, keep my single string but remove everything prior to 'Current'.

FINAL UPDATE

I figured it out, I don't even need substrings at all. you can use str.find() combined with str.erase() to just erase everything up until the found word using size_t pos:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    string phrase = "My Name current community college";

    cout << phrase << endl << endl;

    for (int i = 0; i < phrase.length(); i++)
    {
        if (phrase[i] == ' ')
        {
            phrase[i + 1] = toupper(phrase[i + 1]);
        }
    }

    size_t pos = phrase.find("Current"); //find location of word
    phrase.erase(0,pos); //delete everything prior to location found

    cout << phrase << endl << endl;
    system("PAUSE");
    return 0;

}

You can also replace

size_t pos = phrase.find("Current");
phrase.erase(0,pos);

with just phrase = phrase.substr(phrase.find("Current")); and that works too.

Community
  • 1
  • 1
cma0014
  • 1,517
  • 3
  • 11
  • 9
  • 2
    The topic which you are looking for are regular expressions. You can read through this page to learn about regexp_replace http://www.cplusplus.com/reference/regex/match_replace/ – cerkiewny Jul 11 '14 at 14:50
  • [std::string::find](http://en.cppreference.com/w/cpp/string/basic_string/find) has an override for C strings as well as std::strings. That gives you a position, then just substring to get the remainder. – crashmstr Jul 11 '14 at 14:55

2 Answers2

2

you can use find to find the index to start from, you can use substr to select the portion you want to return.

size_t position = str.find("Current");   

string newString = phrase.substr(position);     // get from position to the end

http://www.cplusplus.com/reference/string/string/substr/

  • ...and find() to search for it (BTW cma0014, your code will fail if last character is a space). – Adriano Repetti Jul 11 '14 at 14:54
  • 1
    @AdrianoRepetti i believe that `find()` finds the beginning of the word – Sam I am says Reinstate Monica Jul 11 '14 at 14:55
  • I was confused about how to use size_t with str.find() to search for more than a single character. I got it to work, thanks for your rapid response. However, I'm obsessive and a perfectionist, so just an A isn't what's important to me: Is there a way to accomplish the same thing by actually trimming my existing string rather than creating a new one? – cma0014 Jul 11 '14 at 14:58
  • @SamIam yes, it'll find "Current" and he'll use that index as input for `substr()`: `phrase.substr(phrase.find("Current"))` – Adriano Repetti Jul 11 '14 at 15:13
  • @cma0014 Why do you think trimming your existing string is actually a more perfect solution? – Sam I am says Reinstate Monica Jul 11 '14 at 15:13
  • @cma0014 IMO it's not better. You can't access directly `std::string` buffer for writing and through `[]` to copy chars back...it'll be very inefficient. – Adriano Repetti Jul 11 '14 at 15:14
  • I don't know, haha. I figured out how to do it simply though. See final update in question. – cma0014 Jul 11 '14 at 15:19
0

You can run a for loop to find the first couple of letters to find Current. Then at the index where it is found, you can take the substring from that index. See the code below:

for (int i = 0; i < phrase.length(); i++){ if (phrase[i] == 'C' && phrase[i + 1] == 'u'){ phrase = phrase.substr(8); } }

More ideally, you might want to use phrase.find("Current") as it will save you the trouble of the for loop.

Joe Urc
  • 487
  • 5
  • 19
  • Thanks for suggestion, I figured out how to get it without loops or substrings though. Both seem reasonable to me though. I'm just trying to expand my knowledge as much as possible. – cma0014 Jul 11 '14 at 15:29