combinantion of string version 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void truncate( char *str );

int main( void )
{
    char str[] = {'a', 'b','c', 'd', ''};
    char *p = str;
    truncate( p );
    return 0;
}

void truncate( char *str )
{
    if( str == NULL )
    {
        return;
    }
    int len = strlen( str );
    if( len == 2 )
    {
        //printf( "%c\n", *( str + len -1 ) );
        return;
    }
    for( int i = 0; i < len; i++ )
    {
        printf("%c ", *( str + i ));
        char *temp = ( char * ) malloc( ( len - 1 ) * sizeof( char ) );
        if( temp == NULL )
        {
            printf( "failed to allocate memory..." );
            exit( 1 );
        }
        if( i == 0 )
        {
            //strncat( temp, str + 1, len - 1 );
            for( int j = 1; j < len; j++ )
            {
                temp[j-1] = *( str + j );
            }
        }
        else
        {
            //strncat( temp, str, i - 1 );
            for( int j = 0; j < i; j++ )
            {
                temp[j] = *( str + j );
            }
            //strncat( temp, str + i + 1, len  - i );
            for( int j = i+1; j < len; j++ )
            {
                temp[j-1] = *( str + j );
            }
        }
        temp[len-1] = '';
        printf( "%s\n", temp );
        truncate( temp );
    }
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s