0

I'm in charge of helping first year students with this program where we need to find a word in a string, a change it by another given word. i wrote it as such

#include <iostream>
#include <string>

using namespace std;

void changeWord(string &texto, string wordB, int pos, int sizeA)
{
    int auxi;
    string antes, depois;
    for(auxi = 0; auxi < pos; auxi++)
    {
        antes[auxi] = texto[auxi];
        cout<< "Antes["<< auxi<< "]: "<< antes[auxi]<< endl;
    }
    for(auxi = 0; auxi+pos+sizeA < texto.length(); auxi++)
    {
        depois[auxi] = texto[auxi+pos+sizeA];
        cout<< "Depois["<< auxi<< "]: "<< depois[auxi]<< endl;
    }
    cout<< "Antes: "<< antes<< "   Depois: "<< depois<< endl;
    texto = antes + wordB + depois;
    cout<< "Texto:"<< texto<< endl;
}

void findWord(string texto, string wordA, string wordB)
{
    int auxi;
    for(auxi = 0; auxi < texto.length(); auxi++)
    {
        cout<< "texto["<< auxi<< "]: "<< texto[auxi]<<"   wordA[0]: "<< wordA[0]<< endl;
        if(texto[auxi] == wordA[0])
        {
            int auxj;
            for(auxj = 1; auxj < wordA.length() && texto[auxi+auxj] == wordA[auxj]; auxj++)
            {
                cout<< "texto["<< auxi+auxj<< "]: "<< texto[auxi+auxj]<< "   wordA["<< auxj<< "]: "<< wordA[auxj]<< endl;
            }
            if(auxj == wordA.length())
            {
                changeWord(texto, wordB, auxi, wordA.length());
            }
        }
    }
}

int main()
{
    string texto = "Isto_e_um_texto_qualquer";
    string wordA, wordB;
    cin >>wordA;
    cin >>wordB;
    findWord(texto, wordA, wordB);
    return 0;
}

I would expect this to work, and to some degree it does what i wanted up until in function call to 'changeWord()' when i try to output both 'antes' and 'depois' strings.

These work inside their respective loops, printing to screen the expected char:

cout<< "Antes["<< auxi<< "]: "<< antes[auxi]<< endl;

cout<< "Depois["<< auxi<< "]: "<< depois[auxi]<< endl;

This does not:

cout<< "Antes: "<< antes<< "   Depois: "<< depois<< endl;

both 'antes' and 'depois' are printed as blank. In addition the program crashes when it gets to this line:

 texto = antes + wordB + depois;

I assume it is for the same reason it cant print them in the previous line. What am I doing wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Guga Figueiredo
  • 292
  • 4
  • 18
  • It would be a nice idea to encourage your students to avoid `using namespace std`... – Rook May 20 '14 at 12:54
  • @Rook: why you think this is a nice idea? declaring `using namespace std;` makes the code more readable, isn't it? – NirMH May 20 '14 at 12:58
  • @NirMH: [See here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), it's not a massive decrease in readability and it prevents unintentional namespace collisions. – The Forest And The Trees May 20 '14 at 13:07
  • @NirMH if the namespace were any longer, perhaps. But 5 additional characters is a small overhead. In return, you avoid polluting the current namespace and remove a bunch of potential namespace collisions. For maintainers and debuggers (and SO answerers), it is obvious where the classes came from, and what they might be expected to do. – Rook May 20 '14 at 13:08
  • @NirMH http://stackoverflow.com/questions/1265039/using-std-namespace also makes good points. Notably, `using namespace` isn't inherently bad, but it shouldn't be used carelessly. – Rook May 20 '14 at 13:10
  • @Rook: thanks for the comment - this was valuable knowledge indeed. i'll need to change my habits... – NirMH May 20 '14 at 20:07

2 Answers2

3

You have undefined behavior in your code.

You declare both antes and depois as empty strings, then you do e.g.

antes[auxi] = texto[auxi];

It doesn't matter what index you use, it will still be out of bounds for the empty string antes.

In that specific loop, you start the indexing from zero, so you could append instead:

antes += texto[auxi];

Or instead of the loop use the substr function:

antes = texto.substr(0, auxi);

And not to talk about the existing replace function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • You know.. i thought about this... but i didnt think this would be ok, since texto[auxi] would be char and not string... im not totaly confortable with c++ yet... still bound by some of the C implementations. – Guga Figueiredo May 20 '14 at 20:30
2

we need to find a word in a string, a change it by another given word.

May I suggest that you use the built in features of std::string to do the hard work for you?

std::string s = "a phrase to be altered";
std::string to_replace = "phrase";
std::string replacement = "sentence";

size_t pos = s.find(to_replace);

if (pos != std::string::npos)
    s.replace(pos, to_replace.length(), replacement);
Rook
  • 5,734
  • 3
  • 34
  • 43
  • This exercize is exactly to teach the students how to perform this without those features... par of the teacher's course or something... I do intend to show them this.. but i must be able to explain to them how to do without it – Guga Figueiredo May 20 '14 at 20:25
  • I've never understood this whole "teaching C++ minus the libraries and all the good language features" thing. Why not just teach C instead? – Rook May 20 '14 at 20:37
  • My thoughts exactly... go figure... as far as i know, this course was supposed to be in C.. i suppose it has something to do with the reasoning involved – Guga Figueiredo May 20 '14 at 21:52