String Erase

I'm trying to write a function that will cut off the last two letters of 1 word and then add the remainder in front of another word. I keep getting this extremely long error when I try to compile and my guess is because I'm implementing the erase string wrong.

1
2
3
4
5
6
7
8
9
10
11
12
Call
EnglishWord = Translate(FirstSide, SecondSide);

Function
void Translate(string word, string piglatin)
{
  string Translated;
  int plen = piglatin.length();
  piglatin.erase(plen-1, 2);
  Translated = piglatin + word;
}
Last edited on
void functions don't return anything, so you can't do this: EnglishWord = Translate(FirstSide, SecondSide);
Last edited on
1
2
3
4
    std::string hack = "Dearp", slash = "Sudo";
    hack.erase(hack.end() - 2, hack.end());
    hack.resize(hack.size() + slash.size());
    std::swap_ranges(hack.begin() + hack.size() - slash.size(), hack.end(), slash.begin());
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <string>
#include <iostream>

std::string mangle(std::string s)
{
    if ( s.length() > 2 )
        std::rotate(s.begin(), s.begin()+2, s.end()) ;

    // TODO:  If the last letter in s is not a vowel, then...

    return s ;
}

int main()
{
    std::cout << mangle("pig") << ' ' << mangle("latin") << '\n' ;
}
I just did:
string Translate(string word, string piglatin)
{
string Translated;
int plen = piglatin.length();
piglatin.erase(piglatin.end() - 2, piglatin.end());
Translated = piglatin + word;
return Translated;
}

but there appears to be a new problem. I have it so that it reads in the first word with a getline until a '-' is encountered and the second word until a ' ' is encountered. It works fine until the end of a line in which it adds an extra space and splits the next word.

sample input:
is-Thay is-ay a-ay ittle-lay est-tay o-tay ee-say
ow-hay e-thay ig-pay atin-lay anslation-tray
ogram-pray orks-way

sample output:
is-thay
is-ay
a-ay
ittle-lay
est-tay
o-tay
ee-say

ow-hay
e-thay
ig-pay
atin-lay
anslation-tray

ogram-pray
orks-way


this
is
a
little
test
to
see
h
ow
the
pig
latin
translation
pr
ogram
waorks

I tried making a while loop so that if the first word read in was a '\n' it would just read in another word, but then I realized that the \n would be on the second word of the last line. Is there a way to check to see if the \n is tacked onto the end?
It would probably be easier to just use cin >> token and split the token into parts, that way there are no special cases to handle.
Initially I read in each piglatin word as 1 word, but then translating them back to English became a real problem for me, because it was hard for me to distinguish between each side of the '-'. I'm trying to use the string.find function to search the second side of the piglatin word for a '\n' right after its read in and to remove it before I put the word through any of my functions. I'm not quite sure how the string.find function works though.
I'm trying something along the lines of:
1
2
if (SecondSide.find("\n")!= string::npos)
     SecondSide.erase(SecondSide.find("\n"));


but its having no effect. Any suggestions?
I'm starting to think I don't know the real problem with why the output comes out the way it does. I know it has something to do with the new line feed. But from looking at my code it would seem that instead of extra end lines the "ay" I'm trying to remove from each word would remain. At this point I could really use some help.
This is what I'm working with at:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  getline(input, FirstSide, Delimeter);
  while (!input.eof()){
    getline(input, SecondSide, Delimit);
    if( SecondSide.find('\n') != string::npos)
      SecondSide.erase(SecondSide.find('\n'));
    LatinWord = FirstSide + "-" + SecondSide;
    LatinArray[Count] = LatinWord;
    EnglishWord = Translate(FirstSide, SecondSide);
    WordArray[Count] = EnglishWord;
    Count++;
    getline(input, FirstSide, Delimeter);
}

string Translate(string word, string piglatin)
{
  string Translated;
  piglatin.erase(piglatin.end() - 2, piglatin.end());
  Translated = piglatin + word;
  return Translated;
}
It looks like you've got find down.

You might be surprised at the output if you insert the lines:

1
2
std::cout << "first side: " << FirstSide << '\n' ;
std::cout << "second side: " << SecondSide << '\n' ;


in between lines 3 and 4.

Again, you would find this simpler if you just used cin >> token and split the token apart.

If I were you, the code above would become:

1
2
while ( input >> token )
    WordArray[Count++] = Translate(token) ;


and Translate would deal with breaking down the token into parts and returning the translated string. (The translation can be accomplished by adding two substrings of token together)
Last edited on
The problem is I havn't the slightest idea how to split a string into two separate strings.
http://www.cplusplus.com/reference/string/string/substr/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std ;

string Translate(const string& token)
{
    string::size_type pos = token.find('-') ;

    if ( pos == string::npos )
        return token ;

    // left is text before the '-'
    string left = token.substr(0, pos) ;

    // right is text after the '-' minus the 2 characters at the end of token.
    string right = token.substr(pos+1, token.size()-(pos+1)-2) ;

    return right + left ;
}

int main()
{
    istringstream input("is-Thay is-ay a-ay ittle-lay est-tay o-tay ee-say\n"
                        "ow-hay e-thay ig-pay atin-lay anslation-tray\n"
                        "ogram-pray orks-way" ) ;

    vector<string> words ;
    string token ;

    while ( input >> token )
        words.push_back(Translate(token)) ;

    for ( unsigned i=0; i<words.size(); ++i )
        std::cout << words[i] << '\n' ;
}
Topic archived. No new replies allowed.