Search code examples
cstringcharansi-c

function invertNumerSubstrings didn´t work


I have this problem in Ansi C

3 Create a function that receives an array of strings of 20 characters each decode, considering the following: a. Reading it from left to right, the digit indicates how many characters, from there, to be invested (between characters to invest can be digits, to that effect, are considered common characters).
b. The digit characters should be replaced with the first character of the string reversed.

Example. The string aj5pr2*dfkl3abc2qwe1azk must be ajd*2rpfklcbawqeazk

Use notation and pointer arithmetic

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

#define TAM 20


char* invertNumerSubstrings(char*);

int main()
{
    char chain[TAM];
    printf("\n Ingrese chain: ");
    gets(chain);
    fflush(stdin);
    char result;
    result=invertNumerSubstrings(chain);
    printf("\n Chain modified: ");
    puts(chain);
    printf("\n");   
    return 0;
}

char* invertNumerSubstrings(char* chain)
{
    int flag =0;
    char *pl= chain;
    char *pe= chain;
    char aux;
    while(*pl=='\0'&& *pe=='\0');
    {
        if(!(*pl=='1')&&(*pe=='9'))
        {
            pl++;
            pe++;
        }
        else
        {

            if(flag ==0)
            {
                pe=*pl;
                flag=1;
                pl--;
            }
            if(*pe<*pl)
            {
                aux=*pl;
                *pl=*pe;
                *pe=aux;
            }
        }
    }
    return *chain;
}

This program don´t have compiling errors but didn´t work


Solution

  • There are number of problems in your code. Pointing some of them. In function main()

    char result;
    result=invertNumerSubstrings(chain);
    

    return type of function invertNumerSubstrings is char* which doesn't match with result's type.

    while(*pl=='\0'&& *pe=='\0');
    

    ; in above statement is logically incorrect which may results in infinite execution of loop in case condition got satisfied. *pl=='\0'&& *pe=='\0' condition is not looking perfect according to need of question (Please correct me if i am wrong).

    return *chain; 
    

    The return statement is last statement of function invertNumerSubstrings whose return type doesn't match with char*.

    To get the desired output, you can try this:

    void invertNumerSubstrings(char* chain)
    {
    
    char *pl= chain;
    char* chain_ptr=chain;   // chain_ptr to hold starting address of chain
    char* final=(char*)malloc(sizeof(chain));
    char* final_ptr=final;  // // final_ptr to hold starting address of final
    memset(final, '\0', sizeof(chain));
    
    while(*pl!='\0')
    {
    
        if(*pl>=49 && *pl<=57) //
        {   
             int shift=*pl-48; // to find the shift amount
             int i=0;
             *pl++;
    
             for(i=shift-1; i>=0; i--){
                 *final=*(pl+i);
                 final++;
             }
           pl=pl+shift;  // seek the pointer to correct position            
        }
    
         else
             {
                  *final=*pl;
                   pl++;
                   final++;
    
             }
    }
    
    chain=chain_ptr; // assign original address of chain to chain again
    
    while(*final_ptr !='\0'){
          *chain=*final_ptr ;
          final_ptr++;
          chain++;             
    }
    *chain='\0';
    
    free(final);
    
    }
    

    Assumption: when an integer number is encountered in string then its subsequent string is having length atleast equal to value of integer.