Supportnet / Forum / Anwendungen(Java,C++...)
Leerzeichen vor String löschen in C/C++
Frage
Hi Leute,
Ich möchte in einem String die anführenden Leerzeichen löschen.
Der String sieht so aus " Lift Technik"
Danach sollte er so aussehen: "Lift Technik"
Die Programmiersprache siehe Betreff.
Vielen Dank im Vorraus.
Rowalind
Antwort 1 von tobi-8
ja mach es halt, wir haben nichts dagegen :-ö)
Antwort 2 von semi
#include <stdio.h>
#include <string.h>
char *ltrim(char *s) {
int i=0;
char *result=s,*tmp=s;
while(*(s++)==' ') i++;
if(i>0) {
result = new char[strlen(s-i)];
strcpy(result,--s);
delete [] tmp;
}
return result;
}
int main() {
char *s1 = " Lift Technik";
char *s2 = "Lift Technik";
s1 = ltrim(s1);
s2 = ltrim(s2);
printf("#%s#\n", s1);
printf("#%s#\n", s2);
return 0;
}
Antwort 3 von semi
Kleiner Fehler, hab' mich vertippt.
result = new char[strlen(s)-i];
result = new char[strlen(s)-i];
Antwort 4 von Rowalind
Danke,
Werde ich gleich mal versuchen.
Gruß Rowalind
Werde ich gleich mal versuchen.
Gruß Rowalind
Antwort 5 von semi
Oh Mann, alles falsch :-)
Jetzt aber.
Jetzt aber.
char *ltrim(char *s) {
char *tmp=s,*result=s;
while(*tmp==' ') tmp++;
if(tmp!=result) {
result = new char[strlen(tmp)];
strcpy(result,tmp);
delete [] s;
}
return result;
}