Char array buchstaben ersetzen
-
hallo,
ich habe ein char array für strings. daraus möchte ich dann einzele buchstaben ersetzen lassen aus deisen 'strings'.char Text[150]; cout << "Text: "; cin.ignore(); cin.get(Text, 149); for (int i=0; i<150; i++) { if (Text[i] == 'a') cout << "b"; }
Dann wird natürlich nur b angezeigt. Doch wie ERSETZE ich ihn richtig?
-
char Text[150]; cout << "Text: "; cin.ignore(); cin.get(Text, 149); for (int i=0; i<150; i++) { if (Text[i] == 'a') cout << "b"; text[i] = "b"; }
wenn ich mich nicht irre..... aber ich bin nicht ganz sicher ob ' oder " aber eins von beidem müsste eigtl funktionieren
-
char input[128]; std::cout << "Text (max. 127 Zeichen): "; std::cin.ignore().get(input, 127); for (std::size_t i(0); i < 128; ++i) if (input[i] == 'a') input[i] = 'b';
-
du stehst doch so auf standard-algorithmen?
#include <algorithm> //... char input[128]; //... std::replace (input, input+128, 'a', 'b');
-
void replace( char* str, char old, char _new ) { while(*str) { if( *str == old ) *str = _new; str++; } } char buf[] = "Klozchuezzel"; replace(buf,'z','s');
-
algorittmus woman mittmus schrieb:
void replace( char* str, char old, char _new ) { while(*str) { if( *str == old ) *str = _new; str++; } } char buf[] = "Klozchuezzel"; replace(buf,'z','s');
Oh ! Ein Rad !! :p
Gruß,
Simon2.
-
@algorittmus woman mittmus:
template <typename iterator_type, typename value_type> void replace(iterator_type first, iterator_type last, value_type const& find, value_type const& value) { while (first != last) { if (*first == find) *first = value; ++first; } }
-
danke ihr habt mir sehr geholfen.
kann man auch aus einem string zb 1 buchstaben für 2 zeichen ersetzen lassen?
-
-
mit char-arrays tust du dir da schwer: zeichen im string suchen, alle nachfolgenden zeichen um die anzahl der neu einzufügenden zeichen nach hinten verschieben und schließlich die neuen zeichen an der stelle einfügen.
mit C++-strings wäre das einfacher:
string foo("test"); foo.replace(foo.find("e"), 1, "foo");
-
Danke, aber somit lässt sich im Stzring alles nur einmal ersetzen. Gibt es noch eine andere Möglichkeit
-
man ich bin blöd... sorry habe den link nicht gesehen da stehts ja
danke!