1

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 ?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Othmane
  • 39
  • 4
  • This is not how strings/arrays work in C. You cannot "store data inside a pointer". See the linked duplicate and also the string FAQ for beginners here: [Common string handling pitfalls in C programming](https://software.codidact.com/posts/284849) – Lundin Jan 04 '22 at 09:14
  • 1
    @Othmane This code snippet char *imei_buffer; strcpy(imei_buffer,'\0'); invokes undefined behavior – Vlad from Moscow Jan 04 '22 at 09:17
  • I tried using normal char arrays[] but it didn't work as well. – Othmane Jan 04 '22 at 09:18
  • yeah, that is right, vs code and gcc didn't show that error. I guess it has something to do with the gcc version. – Othmane Jan 04 '22 at 09:21
  • @Othmane No it has to do with undefined behavior = random bugs occurring or program might seen to work fine even though it has latent bugs. It has nothing to do with the compiler, it is the programmer's responsibility to allocate memory and only copy data into allocated memory. – Lundin Jan 04 '22 at 10:05

1 Answers1

0

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"
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335