String umdrehen

Raetsel

Grünschnabel
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
 
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)
 
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:
string array[10];

//array mit Wörtern füllen...

for(int i=9; i>=0; i--)
    cout<< array[i].c_str();


mfg
umbrasaxum
 
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:
#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
 
Zuletzt bearbeitet:
hallo, danke für die zahlreichen antworten, allerdings hab ich noch eine frage nun
Code:
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....
 
moin


Das brauchst du manchmal, für "Dinge" die nciht richtig mit basic_strings umgehen können.
Definition:
MSDN hat gesagt.:
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
 
Zurück