Search code examples
carraysfunctionpalindrome

Intro to Programming in C - [Final Exam Sample] Palindrome Function


I need help! I can't do it. I tried in many ways but non has worked. At least can someone help me with the LOGIC. Below I will paste my code.

  • Develop a program with a function that receives an array of chars. The function has to modify the original and copy it exactly to another one but putting two asterisks (**) before each word that is a PALINDROME, a palindrome is a word which reads the same backward or forward (e.g ANNA, KAYAK).

For example, my best friend anna has a red kayak

my best friend **anna has a red **kayak.

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

void Cargar(char texto1[])
{
    int i=0;
    char letra;

    do
    {
        letra=getche();
        if(letra != '.')
        {
            texto1[i]=letra;
            i++;
        }
    }
    while (letra != '.' && i<99);

    texto1[i]='\0';

}


int main()
{
    char texto1[100], texto2[100];
    printf("Ingrese texto: ");
    Cargar(texto1);

    int pos=0, esp_anterior=0, aux1=0, aux2=0, aux3=0, bandera=0;
    int i, j, k;

    for(pos = 0 ; texto1[pos] != '\0' ; pos++)
    {
        if( texto1[pos] == ' ')
        {
            if(bandera==0)
            {
                if( hay_palindromo(texto1, pos, esp_anterior) == 1)
                {
                    texto2[0]='*';
                    texto2[1]='*';

                    for(i=2, j=0; j<pos ; j++ , i++)
                    {
                        texto2[i]=texto1[j];
                    }
                    aux1=i;
                    aux2=j;
                }
                else
                {
                    for(i=0; i<pos; i++)
                    {
                        texto2[i]=texto1[i];
                    }
                    aux3=i;
                }
                bandera = 1;
                esp_anterior = pos;
            }

            else

            {
                if(bandera == 1)
                {
                    if( hay_palindromo(texto1, pos, esp_anterior) == 1)
                    {

                        texto2[aux1]='*';
                        texto2[aux1+1]='*';

                        for(i=aux1+2, j=aux2; j<pos ; j++ , i++)
                        {
                            texto2[i]=texto1[j];
                        }
                        aux1=i;
                        aux2=j;

                    }
                    else
                    {
                        for(i=aux3; i<pos; i++)
                        {
                            texto2[i]=texto1[i];
                        }
                        aux3=i;

                    }
                    esp_anterior=pos;

                }
            }

        }
    }
    printf("\n%s", texto2);

    return 0;
}

int hay_palindromo(char texto1[], int pos, int esp_anterior)
{
    int i, j, bandera=0;
    for(i=esp_anterior, j=pos; i<pos; i++, j--)
    {
        if(texto1[i] == texto1[j])
        {
            bandera=1;
        }
        else
        {
            bandera=0;
            break;
        }
    }
    if(bandera==1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

Solution

  • I would suggest a logic like that:

    Take string, separate it by a delimiter - " " (space) in your case with strtok.

    Then, send each delimited word to a function that determines whether the string is a palindrome or not.

    If the string is palindrome, allocate enough space for the string + "", and copy "" + string into the allocated space, save the current position of your 'cursor' on that array. Pseudo Example:

    "Anna notpali"
    char *new_str;
    int cursor = 0;
    
    is_palindrome("Anna") 
          Yes -> new_str = malloc(strlen("Anna") + strlen("**") + 1)
          strcpy(&(new_str[cursor]),"**Anna");
          cursor  += strlen("**Anna");
    
    is_palindrom("notpali")
          No -> new_str = realloc(new_str,strlen(" notpali") + 1)
          strcpy(&(new_str[cursor])," notpali");
          cursor += strlen(" notpali");
    
    // After going through all the words
    new_str[cursor] = '\0'
    

    etc, there might be some corner cases to take care of, but this is a basic logic I suggest to approach it.