Salve a tutti ,
mi pare che il titolo si spieghi da solo. Ho la necessità di aggiungere un carattere in testa ad una stringa. Esiste una funzione di libreria predefinita?
Grazie
Salve a tutti ,
mi pare che il titolo si spieghi da solo. Ho la necessità di aggiungere un carattere in testa ad una stringa. Esiste una funzione di libreria predefinita?
Grazie
strcat
No MP tecnici (non rispondo nemmeno!), usa il forum.
avevo provato una cosa del genere:
peccato debba appendere di tanto in tanto il carattere "/" e con la prima chiamata a strcat c a quel punto non contiene più solo "/". Potrei risettare c perché sia "/" dopo ogni chiamata, ma non mi sembra il massimo della pulizia.codice:char c[] = "/"; string = strcat (c, string);
Se la stringa ha memoria sufficiente allocata puoi anche fare:
codice:int l=strlen(string); string[l]=c; string[l+1]=0;
hai provato con qualcosa di questo tipo?codice:char *appendBefore(char *strSrcBuffer, char *strToAppend) { unsigned int iSrcBufferLen = 0; unsigned int iStrToAppendLen = 0; char *strOutBuffer = NULL; if ( NULL == strSrcBuffer) return NULL; if ( NULL == strToAppend) return NULL; iSrcBufferLen = strlen(strSrcBuffer); iStrToAppendLen = strlen(strToAppend); strOutBuffer = (char *)malloc((iSrcBufferLen + iStrToAppendLen + 1)*sizeof(char)); if ( NULL == strOutBuffer ) return NULL; strcpy(strOutBuffer, strToAppend); strcpy(strOutBuffer+iStrToAppendLen, strSrcBuffer); strOutBuffer[iSrcBufferLen+iStrToAppendLen] = '\0'; return strOutBuffer; } int main(void) { char *slash = "/"; char *tmp = "la mia stringa"; char *result = appendBefore(tmp, slash); }
ps. l'ho scritta al volo, potrebbe essermi sfuggito qualcosa.
Administrator of NAMDesign.Net
Non ho mica capito qual è il problema ...Originariamente inviato da Frank Lioty
avevo provato una cosa del genere:
peccato debba appendere di tanto in tanto il carattere "/" e con la prima chiamata a strcat c a quel punto non contiene più solo "/". Potrei risettare c perché sia "/" dopo ogni chiamata, ma non mi sembra il massimo della pulizia.codice:char c[] = "/"; string = strcat (c, string);
Se scrivi
non risolvi?codice:strcpy(dest, "\\"); strcat(dest, string);
No MP tecnici (non rispondo nemmeno!), usa il forum.
Quello risolverebbe il problema del carattere divisore in effetti. Il problema è che devo fare una cosa di questo tipo:
e partendo da string4, andando a ritroso, dovrei costruire la stringa:codice:char *string1 = "primo" char *string2 = "secondo" char *string3 = "terzo" char *string4 = "quarto"
codice:primo\secondo\terzo\quarto
Hai provato ad usare sprintf?
SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
Di questo libro e degli altri (blog personale di recensioni libri) | NO M.P. TECNICI
ripeto:
codice:#include "stdio.h" #include "stdlib.h" #include "string.h" char *appendBefore(char *strSrcBuffer, char *strToAppend) { unsigned int iSrcBufferLen = 0; unsigned int iStrToAppendLen = 0; char *strOutBuffer = NULL; if ( NULL == strSrcBuffer) return NULL; if ( NULL == strToAppend) return NULL; iSrcBufferLen = strlen(strSrcBuffer); iStrToAppendLen = strlen(strToAppend); strOutBuffer = (char *)malloc((iSrcBufferLen + iStrToAppendLen + 1)*sizeof(char)); if ( NULL == strOutBuffer ) return NULL; strcpy(strOutBuffer, strToAppend); strcpy(strOutBuffer+iStrToAppendLen, strSrcBuffer); strOutBuffer[iSrcBufferLen+iStrToAppendLen] = '\0'; return strOutBuffer; } int main(void) { char *slash = "/"; char *tmp1 = "prima"; char *tmp2 = "seconda"; char *tmp3 = "terza"; char *tmp4 = "quarta"; char *result = appendBefore(tmp4, slash); result = appendBefore(result, tmp3); result = appendBefore(result, slash); result = appendBefore(result, tmp2); result = appendBefore(result, slash); result = appendBefore(result, tmp1); printf(result); }
Administrator of NAMDesign.Net
Questo diventa un altro problema e la cosa migliore - come ti ha suggerito Alex - è usare la sprintf (o meglio, per sicurezza, la snprintf)Originariamente inviato da Frank Lioty
Quello risolverebbe il problema del carattere divisore in effetti. Il problema è che devo fare una cosa di questo tipo:
e partendo da string4, andando a ritroso, dovrei costruire la stringa:codice:char *string1 = "primo" char *string2 = "secondo" char *string3 = "terzo" char *string4 = "quarto"
codice:primo\secondo\terzo\quarto
No MP tecnici (non rispondo nemmeno!), usa il forum.