Leerzeichen einfügen
-
Hallo,
zur Abwechslung mal ne sehr schlaue Frage für Anfänger
Ich habe einen Char *, der mit Text gefüllt ist. Jetzt möchte ich davor 3 Leerzeichen einfügen. Aber ich schaffe es nicht wirklich
Danke für Eure Antworten!
-
Ungetestet:
#include <iostream> #include <cstring> using namespace std; int main() { char str[100] = "Hallo"; cout << "str=" << str << endl; char *c = new char[strlen(str) + 4]; strcpy(c, " "); strcat(c, str); strcpy(str, c); delete[] c; cout << "str=" << str << endl; cin.get(); return 0; }
Alternativ:
#include <iostream> #include <string> using namespace std; int main() { char str[100] = "Hallo"; cout << str << endl; strcpy(str, (" " + string(str)).c_str()); cout << str; cin.get(); return 0; }
-
const char* a = "..."; string str(" "); str += a;