zeichenkette in string ersetzen



  • steven23 schrieb:

    Immer wenn ich das Script ausführe, hängt es sich auf?

    Ja. Du greifst auf Speicher zu, der nicht dir gehört. Überprüf' doch 'mal die Werte von meinString[ i + 1 ]:

    for( int i = 0; i < laenge; i++ ) {
    
        if( meinString[ i ] == '*' ) {
    
            meinString.replace(meinString.find( "*" ), 2, tabelle[ meinString[ i + 1 ] ] );
            //                                  ^
            // wieso ein String als Argument für string::find( )?
    
            i=0;
        }
    }
    

    Fällt dir was auf!?

    steven23 schrieb:

    Weiß jemand eine Lösung?

    Ja:

    #include <vector>
    #include <string>
    #include <locale>
    #include <iostream>
    
    using namespace std;
    
    int main( )
    {
        vector< string > table;
        table.push_back( "Strasse" );
        table.push_back( "Weg" );
        table.push_back( "Bruecke" );
        table.push_back( "Tunnel" );
    
        string text = "Ich fahre eine *0 entlang,\n"
                      "ueber eine *2 und biege rechts in einen kleinen *1 ein,\n"
                      "der durch einen *3 fuehrt.";
    
        cout << "Text to replace with tokens:" << endl;
        cout << text << endl << endl;
    
        locale germany( "German_Germany" );
    
        string::size_type offset = 0;
    
        for( vector< string >::size_type i = 0; i < table.size( ); ++i ) {
    
            offset = text.find( '*', offset );
    
            if( isdigit( text[ offset + 1 ], germany ) ) {
    
                text.replace( offset++, 2, table[ text[ offset ] - '0' ] );
            }
        }
    
        cout << text << endl;
    }
    

    Rück' das nächste mal vernünftig ein und benutz "cpp"-Tags!

    Greetz, Swordfish



  • ah, jetzt hab ich´s geschnallt... besten dank



  • mmm, ein problem hab ich allerdings noch. die for schleife richtet sich jetzt nach der anzahl der begriffe im vector. leider habe ich auch manchmal strings, bei denen nicht alle begriffe aus dem tabel benutzt werden. die for schleife müsste also eher den string nach * durchsuchen... aber wie?



  • p.s. die erste version von dir funktionierte. nach deiner überarbeitung gehts nun leider nicht mehr. folgende fehlermeldung spuckt er aus: cpp:3: locale: No such file or directory ???



  • Ist auch keine Hexerei:

    #include <vector>
    #include <string>
    #include <locale>
    #include <iostream>
    
    using namespace std;
    
    int main( )
    {
        vector< string > table;
        table.push_back( "Strasse" );
        table.push_back( "Weg" );
        table.push_back( "Bruecke" );
        table.push_back( "Tunnel" );
    
        string text = "Ich fahre eine *0 entlang,\n"
                      "ueber eine *2 und biege rechts in einen kleinen *1 ein,\n"
                      "der durch einen *3 fuehrt.";
    
        cout << "Text to replace with tokens:" << endl;
        cout << text << endl << endl;
    
        locale germany( "German_Germany" );
    
        string::size_type offset = text.find( '*' );
    
        while( offset != text.npos ) {
    
            if( isdigit( text[ offset + 1 ], germany ) ) {
    
                text.replace( offset, 2, table[ text[ offset + 1 ] - '0' ] );
            }
    
            offset = text.find( '*', ++offset );
        }
    
        cout << text << endl;
    }
    

    Greetz, Swordfish



  • danke, aber wie gesagt, selber fehler... er hat ein problem mit "#include <locale>" glaube ich... ???



  • Dann unterstützt dein Compiler den C++ Standard ISO/IEC 14882:1998 nicht!

    ISO/IEC 14882:1998 schrieb:

    17.4.1.2 Headers

    1 The elements of the C++ Standard Library are declared or defined (as appropriate) in a header.
    2 The C++ Standard Library provedes 32 C++ headers, as shown in Table 11:

    Table 11 - C++ Library Headers

    <algorithm>   <iomanip>    <list>       <ostream>      <streambuf>
    <bitset>      <ios>        <locale>     <queue>        <string>
    <compex>      <iosfwd>     <map>        <set>          <typeinfo>
    <deque>       <iostream>   <memory>     <sstream>      <utility>
    <exception>   <istream>    <new>        <stack>        <valarray>
    <fstream>     <iterator>   <numeric>    <stdexcept>    <vector>
    <functinoal>  <limits>
    

    [...]

    Welchen Compiler benutzt du?

    Verwende inzwischen:

    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main( )
    {
        vector< string > table;
        table.push_back( "Strasse" );
        table.push_back( "Weg" );
        table.push_back( "Bruecke" );
        table.push_back( "Tunnel" );
    
        string text = "Ich fahre eine *0 entlang,\n"
                      "ueber eine *2 und biege rechts in einen kleinen *1 ein,\n"
                      "der durch einen *3 fuehrt.";
    
        cout << "Text to replace with tokens:" << endl;
        cout << text << endl << endl;
    
        string::size_type offset = 0;
    
        for( vector< string >::size_type i = 0; i < table.size( ); ++i ) {
    
            offset = text.find( '*', offset );
    
            if( isdigit( text[ offset + 1 ] ) ) {
    
                text.replace( offset++, 2, table[ text[ offset ] - '0' ] );
            }
        }
    
        cout << text << endl;
    }
    

    bzw.

    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main( )
    {
        vector< string > table;
        table.push_back( "Strasse" );
        table.push_back( "Weg" );
        table.push_back( "Bruecke" );
        table.push_back( "Tunnel" );
    
        string text = "Ich fahre eine *0 entlang,\n"
                      "ueber eine *2 und biege rechts in einen kleinen *1 ein,\n"
                      "der durch einen *3 fuehrt.";
    
        cout << "Text to replace with tokens:" << endl;
        cout << text << endl << endl;
    
        string::size_type offset = text.find( '*' );
    
        while( offset != text.npos ) {
    
            if( isdigit( text[ offset + 1 ] ) ) {
    
                text.replace( offset, 2, table[ text[ offset + 1 ] - '0' ] );
            }
    
            offset = text.find( '*', ++offset );
        }
    
        cout << text << endl;
    }
    

    Greetz, Swordfish



  • habs nun so gemacht und es funktioniert... wuuste sowieso nicht, wozu die local da ist!?

    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main( )
    {
        vector< string > table;
        table.push_back( "Strasse" );
        table.push_back( "Weg" );
        table.push_back( "Bruecke" );
        table.push_back( "Tunnel" );
    
        string text = "Ich fahre eine *0 entlang,\n"
                      "ueber eine *2 und biege rechts in einen kleinen *1 ein,\n"
                      "der durch einen *3 fuehrt.";
    
        string::size_type offset = text.find( '*' );
    
        while( offset != text.npos ) {
    
               text.replace( offset, 2, table[ text[ offset + 1 ] - '0' ] );
               offset = text.find( '*', ++offset );
        }
    
        cout << text << endl;
        system("PAUSE");
    }
    


  • ahja, danke... der zweite funktioniert. ich benutze dev-c++
    danke.



  • Zeichen können in verschiedenen Sprachen anders interpretiert werden. Für verschiedene Zeichensätze gibt es <locale>.

    Zu prüfen, ob das Zeichen nach '' eine Ziffer ist kann auf jeden Fall nicht schaden, da du, wenn du '' als "normalen" text verwendest Probleme bekommst. Weiters wäre es nett, zu Prüfen, ob der Index für deinen vector überhaupt gültig ist.

    Greetz, Swordfish


Anmelden zum Antworten