Zadanie 28
Ostatnio zmodyfikowano 2014-07-02 14:33
snajperek130 Temat założony przez niniejszego użytkownika |
git » 2014-07-02 14:33:13 Dobra, rozwiazalem problem. Co ciekawe bylo tu winne zmienne, a takze kolejnosc insert i erase. Bardzo prosty blad, a jednak ... W tym przypadku erase jest przed insert, wiec erase wytnie tak jakby </b> i insert nie bedzie juz widzialo tej frazy, co spowoduje wyzej napisany blad. sTekst.erase( sTekst.find( "</b>" ), 4 ); sTekst.insert( sTekst.find( "</b>" ), "[/b] " );
Poprawny kod: #include <iostream> #include <string> #include<cstdlib> using namespace std;
string konwertuj( string & sTekst ) { for( int i = 0; i < sTekst.size(); i++ ) { if( sTekst.find( "<b>" ) != string::npos ) { sTekst.insert( sTekst.find( "<b>" ), "[b]" ); sTekst.erase( sTekst.find( "<b>" ), 3 ); } if( sTekst.find( "</b>" ) != string::npos ) { sTekst.insert( sTekst.find( "</b>" ), "[/b] " ); sTekst.erase( sTekst.find( "</b>" ), 4 ); } if( sTekst.find( " " ) != string::npos ) { sTekst.erase( sTekst.find( " " ), 1 ); } } return sTekst; } int main() { string tekst = "<b><b>to jest </b></b> testowy napis :) lol<b>"; cout << konwertuj( tekst ) << endl; return 0; }
Dzieki pekfos, duzo mi pomogles :), pozdro |
|
1 « 2 » |