简体   繁体   中英

Appending char arrays using strcat

I am working on a small encryption algorithm that does this:

converting:

    input = 356307042441013    to output = 333536333037303432343431303133

and it works like this: if a number is equal to 3, it changes it with 333 except if it is in the last position of the string. if not, we add 3 between every two numbers that are different from 3

    input =   3  5   6  3  0   7   0   4   2   4   4   1   0   1   3    
    to 
    output = 333 5 3 6 333 0 3 7 3 0 3 4 3 2 3 4 3 4 3 1 3 0 3 1 3 3

to do so, I wrote this code:

int main() {
    const char *imei = "362162356836934568";
    char *imei_buffer;
    strcpy(imei_buffer,'\0');

    int i = 0;
    while (i < strlen(imei))
    {
        if (i <= (strlen(imei) - 1))
        {
            if (imei[i] == '3')
            {
                strcat(imei_buffer, "333");
            }
            else if ((imei[i] != '3') && (imei[i + 1] == '3'))
            {
                strcat(imei_buffer, &imei[i]);
                //strcat(imei_buffer,'3');
            }
            else if ((imei[i] != '3') && (imei[i + 1] != '3'))
            {
                strcat(imei_buffer, &imei[i]);
                strcat(imei_buffer, "3");
            }
        }
        else if (i == strlen(imei))
        {
            if (imei[i] == '3')
            {
                strcat(imei_buffer, "333");
            }
            else if (imei[i] != '3')
            {
       else if (i == strlen(imei))
        {
                strcat(imei_buffer, &imei[i]);
                //strcat(imei_buffer,'3');
        }
             i++;
        }

        printf("imei_buffer : %s",imei_buffer);

    return 0;
}

The code executes with no error, but it shows no result at the end.

Where I could have gone wrong in this implementation?

You declared a pointer

char *imei_buffer;

that is not initialized and has an indeterminate value.

So the next statement

strcpy(imei_buffer,'\0');

at once invokes undefined behavior.

Moreover the function expects arguments of the type char * while you supplied the second argument of the type char .

This call of the function strcat

strcat(imei_buffer, &imei[i]);

is logically incorrect because it appends a whole substring of the string imei starting with the index i .

What you need is at first to determine the size of the result string, allocate a character array of the size and then fill it with characters of the source string according to the algorithm.

Here is a demonstration program.

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

char * encrypt( const char *s )
{
    size_t n = strlen( s ) + 1;
    
    for ( const char *p = s; *p; ++p )
    {
        if ( p[0] == '3' )
        {
            n += p[1] == '\0' ? 1 : 2;
        }
        else
        {
            if ( p[1] != '3' && p[1] != '\0'  ) ++n;
        }
    }
    
    char *result = malloc( n );

    if ( result != NULL )
    {
        char *p = result;
        do
        {
            *p++ = *s;
            
            if ( s[0] == '3' )
            {
                *p++ = '3';
                if ( s[1] != '\0' ) *p++ = '3';
            }
            else if ( s[1] != '\0' && s[1] != '3' )
            {
                *p++ = '3';
            }
        } while ( *s++ );
    }
    
    return result;
}

int main( void )
{
    const char *s = "362162356836934568";
    
    printf( "\"%s\"\n", s );
    
    char *result = encrypt( s );
    
    if ( result ) printf( "\"%s\"\n", result );
    
    free( result );
}

The program output is

"362162356836934568"
"333632313632333536383336393334353638"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM