|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h>
#include <string.h>
int main ()
{
char textarray[100]="Heute Montag, der 14. Dezember 2004 ist ein guter Tag!";
int zeichen=0;
int j, i, l=0, laenge, positionleer=0;
laenge=strlen(textarray);
for (j=0; j<laenge; j++)
{
zeichen=textarray[j];
if (zeichen==32)
{
for (i=positionleer; i<=laenge; i++)
{
textarray[l]=textarray[i+1];
l++;
}
}
positionleer++;
}
printf("%s\n", textarray);
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
int i = 0;
char a1[] = "Dies ist ein String, der in viele Einzelstrings zerlegt werden soll!";
char **z = NULL;
char* sta = a1;
int idx = 0;
// Alle Teilstrings von a1 bis ausschließlich "Tag!"
for( i = 0; i < strlen( a1 ); i++ ) {
if( a1[ i ] == ' ' ) {
z = ( char** )realloc( z, ( idx + 1 ) * sizeof( char** ) );
z[ idx ] = ( char* )malloc( a1 + i - sta + 1 );
memset( z[ idx ], 0, a1 + i - sta + 1 );
strncpy( z[ idx ], sta, a1 + i - sta );
sta = a1 + i + 1;
idx++;
i++;
}
}
// Jetzt noch der letzte String, dem ja kein Leerzeichen folgt
z = ( char** )realloc( z, ( idx + 1 ) * sizeof( char** ) );
z[ idx ] = ( char* )malloc( strlen( a1 ) + 1 );
strncpy( z[ idx ], sta, strlen( a1 ) + 1 );
// Ausgeben und freigeben
for( i = 0; i <= idx ; i++ ) {
printf( "String #%i: %s (%i)\n", i, z[ i ], strlen( z[ i ] ) );
free( z[ i ] );
}
free( z );
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( void )
{
char str[] = "Dieser String soll zerteilt werden! Dieser String soll zerteilt werden! 1 2 3 4 5";
char* pos = str;
char* nspace = NULL;
char** z = NULL;
int idx = 0;
int i;
while( ( nspace = index( pos, ' ' ) ) ) {
z = ( char** )realloc( z, ( idx + 1 ) * sizeof( char** ) );
z[ idx ] = ( char* )malloc( nspace - pos + 1 );
memset( z[ idx ], 0, nspace - pos + 1 );
strncpy( z[ idx ], pos, nspace - pos );
idx++;
pos = nspace + 1;
}
z[ idx ] = ( char* )malloc( strlen( pos ) + 1 );
strncpy( z[ idx ], pos, strlen( pos ) + 1 );
for( i = 0; i <= idx; i++ ) {
printf( "String #%i: %s (%i)\n", i, z[ i ], strlen( z[ i ] ) );
free( z[ i ] );
}
free( z );
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h>
#include <string.h>
int main ()
{
char textarray[100]="Heute Montag, der 14. Dezember 2004 ist ein guter Tag!";
int zeichen=0;
int j, i, l=0, laenge, positionleer=0;
laenge=strlen(textarray);
printf ("%d", laenge);
for (j=0; j<laenge; j++)
{
zeichen=textarray[j];
if (zeichen==32)
{
for (i=positionleer; i<=laenge; i++)
{
textarray[l]=textarray[i+1];
l++;
}
}
positionleer++;
laenge=strlen(textarray);
}
printf("%s\n", textarray);
printf ("%d", laenge);
return 0;
}
|
Forum Software: Burning Board® 3.1.2, developed by WoltLab® GmbH