tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
7
ZUGRIFFE
4147
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Raetsel Raetsel ist offline Mitglied
    Registriert seit
    Mar 2005
    Beiträge
    11
    Hallo, ich hab eine Frage bezüglich Strings. Und zwar hab ich ein String array[] vorliegen und unter jedem Arraypunkt sind 4 bis 5 Wörter gespeichert. Ich such da nun nach einer Möglichkeit, das letzte Wort als erstes auszugeben.

    Beispiel
    array[1]=" rot, gruen, blau, gelb, schwarz";
    //jetzt wird geändert
    array[1] =" schwarz, rot, gruen, blau, gelb";
    Kann mir jemand nen Tipp geben, wie man das möglichst einfach hinbekommt? Danke
     

  2. #2
    thooomy thooomy ist offline Mitglied Gold
    Registriert seit
    Apr 2005
    Beiträge
    111
    mit C (ich nehm mal an kein C++ sonst würdest du bestimmt strings dazu nehmen)
    kannst du per sscanf() nach den Kommas suchen und die und jedes wort drumherum extra in
    ein char array speichern, danach gemütlich in das array neu einsortieren

    (übrigens kannst du nicht array[1] = " viel text" eingeben - denn die 1 bedeutet in dem Fall das es nur 2 elemente haben darf - so erhälst du Speicherüberläufe)
     

  3. #3
    Registriert seit
    Apr 2002
    Ort
    Delmenhorst (Niedersachsen)
    Beiträge
    3.567
    moin


    Anders als thoomy gehe ich mal davon aus das du String benutzt, da du es ja so geschrieben hattest.

    Du brauchst einfach ne Schleife und gut is:
    Code :
    1
    2
    3
    4
    5
    6
    7
    
     
    string array[10];
     
    //array mit Wörtern füllen...
     
    for(int i=9; i>=0; i--)
        cout<< array[i].c_str();


    mfg
    umbrasaxum
     

  4. #4
    Registriert seit
    Oct 2003
    Beiträge
    1.706
    Hallo,

    @thoomy: Raetsel hat nicht geschrieben von welchem Typ seine var. array ist...

    @umbra:
    Bei dir werden die Wörter einfach in der umgedrehten Riehenfolge ausgegeben...
    Ich nehme mal an Raetsel will einen Rotation seines Wortes nach rechts...

    Code :
    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
    
    #include <iostream>
     
    using namespace std;
     
    /**
     * rotates the subwords in a given word rightwards.
     * @param toRotate the word which should be rotated rightwards (inout para)
     * @param word_delim the delimitator which divides the word in to subwords
     */
    void rwr(string& toRotate, const string& word_delim){
     
            string::size_type del_pos = toRotate.rfind(word_delim);
            if(del_pos != string::npos){
                    string rot = toRotate.substr(del_pos);
                    toRotate.replace(del_pos,rot.size(), "");
                    toRotate.insert(0, rot.substr(word_delim.size()) + word_delim);
            }
     
    }
     
    int main(){
     
            string word = "rot,gruen,blau,gelb,schwarz";
            cout << word << endl;
            rwr(word, ",");
            cout << word << endl;
            return 0;
    }

    Geht bestimmt auch einfacher also einfachere Lösung sind willkommen...

    Gruß

    RedWing
    Geändert von RedWing (06.06.05 um 16:03 Uhr)
     
    "I'm not deaf, I'm ignoring you"
    ----

  5. #5
    Registriert seit
    Apr 2002
    Ort
    Delmenhorst (Niedersachsen)
    Beiträge
    3.567
    moin


    Stimmt hab überlesen das er nur das letzte Wort nach vorne haben will.


    mfg
    umbrasaxum
     

  6. #6
    thooomy thooomy ist offline Mitglied Gold
    Registriert seit
    Apr 2005
    Beiträge
    111
    fürs archiv: bitte alle die fragen erst richtig lesen bevor sie antworten.. vor allem ich
     

  7. #7
    Raetsel Raetsel ist offline Mitglied
    Registriert seit
    Mar 2005
    Beiträge
    11
    hallo, danke für die zahlreichen antworten, allerdings hab ich noch eine frage nun
    for(int i=9; i>=0; i--)
    cout<< array[i].c_str();
    und zwar, zum c_str(); ich hab da jetzt mal nach gesucht und kann nicht so recht ne definition finden, was das genau macht....
     

  8. #8
    Registriert seit
    Apr 2002
    Ort
    Delmenhorst (Niedersachsen)
    Beiträge
    3.567
    moin


    Das brauchst du manchmal, für "Dinge" die nciht richtig mit basic_strings umgehen können.
    Definition:
    Zitat Zitat von MSDN
    Converts the contents of a string as a C-style, null-terminated string.

    const value_type *c_str( ) const;
    Return Value
    A pointer to the C-style version of the invoking string.

    Remarks
    Objects of type string belonging to the C++ template class basic_string<char> are not necessarily null terminated. The null character ' \0 ' is used as a special character in a C-string to mark the end of the string but has not special meaning in an object of type string and may be a part of the string just like any other character. There is an automatic conversion from const char* into strings, but the string class does not provide for automatic conversions from C-style strings to objects of type basic_string<char>.

    The returned C-style string should not be modified, as this could invalidate the pointer to the string, or deleted, as the string has a limited lifetime and is owned by the class string.

    mfg
    umbrasaxum
     

Ähnliche Themen

  1. Einen String umdrehen und vergleichen
    Von FaNo86 im Forum C/C++
    Antworten: 6
    Letzter Beitrag: 10.11.09, 07:50
  2. satz umdrehen
    Von v3rst0rk3r im Forum C/C++
    Antworten: 4
    Letzter Beitrag: 11.04.06, 17:27
  3. Postkarte umdrehen ?!
    Von dariel im Forum Creative Lounge
    Antworten: 4
    Letzter Beitrag: 31.12.05, 12:17
  4. Bilderfolge umdrehen ?
    Von BeaTBoxX im Forum Flash Plattform
    Antworten: 1
    Letzter Beitrag: 21.11.03, 22:48
  5. String umdrehen
    Von ShinmA im Forum Delphi, Kylix, Pascal
    Antworten: 2
    Letzter Beitrag: 23.09.03, 13:22