how to solve matrix vector code problems?

Asked

Viewed 32 times

-1

Hello, could you help me solve this problem in my code ? In the case it was asked to develop an algorithm that performs an encryption on a text. The algorithm must invert the characters of each line of the text. That is, in a given line the character at position 0 will take position n - 1, character 1 will be n - 2 and so on, where n is the line size. Additionally the algorithm when identifying a vowel will replace following the table below:

Vowel Character that replaces to or A @ and or AND # i or I ! o or O * u or U $

Restrictions and rules of your program: • Assume that the input text will not exceed 32 lines, and that the size of each line will not have more than 128 characters counting with the ? 0' • To finish reading the input text the user will enter "FIM_TEXTO", that should not enter the final matrix • No need to treat any accentuation

My code is this, but I don’t know where I’m going wrong.

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

#define MAX_LINHAS 32
#define MAX_CARACTERES 128



typedef struct

{
    char frase[MAX_LINHAS];

} dados;
int main ()
{

    char texto[MAX_LINHAS][MAX_CARACTERES];

    int i, j =0;
    printf ("Digite uma linha em branco para sair do programa.\n");

    for (int i =0; i< MAX_LINHAS ; i++) // Gera a matriz
    {
        printf ("%d",i+1);
        fgets(texto[i],MAX_CARACTERES, stdin);
        if (texto[i][0] == '\n')
        {
            break;
        }
    }

    for (j=0; j<i; j++)
    {
        printf ("%03d",j+1,texto [j]);


    }


    system("pause");
    return 0;
}

void criptografa(dados *cripto)
{
    printf("DIGITE UMA FRASE: \n");
    gets(cripto->frase);


    for(int i = 0 ; i < strlen(cripto->frase) ; i++)
    {
        if(cripto->frase[i] == 'a')
        {
            cripto->frase[i] = '@';
        }
        else  if(cripto->frase[i] == 'e')
        {
            cripto->frase[i] = '#';
        }
        else if (cripto->frase[i] == 'i')
        {
            cripto->frase[i] = '!';
        }
        else if (cripto->frase[i] == 'o')
        {
            cripto->frase[i] = '*';
        }
        else if (cripto->frase[i] == 'u')
        {
            cripto->frase[i] = '$';
        }
        else  if(cripto->frase[i] == 'A')
        {
            cripto->frase[i] = '@';
        }
        else  if(cripto->frase[i] == 'E')
        {
            cripto->frase[i] = '#';
        }
        else if (cripto->frase[i] == 'I')
        {
            cripto->frase[i] = '!';
        }
        else if (cripto->frase[i] == 'O')
        {
            cripto->frase[i] = '*';
        }
        else if (cripto->frase[i] == 'U')
        {
            cripto->frase[i] = '$';
        }
        else
        {
            cripto->frase[i] = cripto->frase[i] ;
        }

    }

    for(int i = 0 ; i< strlen(cripto->frase); i++)
    {
        putchar(cripto->frase[i]);
    }
    printf("\n\n");

     for(int i = strlen(cripto->frase) ; i>=0 ; i--)
    {
        putchar(cripto->frase[i]);
    }
    printf("\n\n");

    //IMPRIME TEXTO:

    printf("TEXTO DEPOIS DE PROCESSADO: \n");
    for(int i = 0; i< tamanho; i++)
    {
        printf("LINHA %d: %s\n", i, texto[i]);
    }

}

1 answer

0

We can create the struct and call the encryption() function. To invert a string we have the _strrev method().

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

#define MAX_LINHAS 32
#define MAX_CARACTERES 128

typedef struct dados

{
    char frase[MAX_LINHAS][MAX_CARACTERES];

} dados;

void criptografa(dados* cripto);

int main()
{
    dados texto;

    int i, j = 0;
    printf("Digite uma linha em branco para sair do programa.\n");

    for (int i = 0; i < MAX_LINHAS; i++) // Gera a matriz
    {
        printf("%d", i + 1);
        fgets(texto.frase[i], MAX_CARACTERES, stdin);
        if (texto.frase[i][0] == '\n')
        {
            break;
        }
    }

    criptografa(&texto);

    system("pause");
    return 0;
}

void criptografa(dados* cripto)
{
    for (int i = 0; i < strlen(cripto->frase); i++) 
    {
        for (int j = 0; j < strlen(cripto->frase[i]); j++) 
        {
            if (cripto->frase[i][j] == 'a')
            {
                cripto->frase[i][j] = '@';
            }
            else  if (cripto->frase[i][j] == 'e')
            {
                cripto->frase[i][j] = '#';
            }
            else if (cripto->frase[i][j] == 'i')
            {
                cripto->frase[i][j] = '!';
            }
            else if (cripto->frase[i][j] == 'o')
            {
                cripto->frase[i][j] = '*';
            }
            else if (cripto->frase[i][j] == 'u')
            {
                cripto->frase[i][j] = '$';
            }
            else  if (cripto->frase[i][j] == 'A')
            {
                cripto->frase[i][j] = '@';
            }
            else  if (cripto->frase[i][j] == 'E')
            {
                cripto->frase[i][j] = '#';
            }
            else if (cripto->frase[i][j] == 'I')
            {
                cripto->frase[i][j] = '!';
            }
            else if (cripto->frase[i][j] == 'O')
            {
                cripto->frase[i][j] = '*';
            }
            else if (cripto->frase[i][j] == 'U')
            {
                cripto->frase[i][j] = '$';
            }
        }
    }

    for (int i = 0; i < strlen(cripto->frase); i++)
    {
        strcpy_s(cripto->frase[i], sizeof(cripto->frase[i]), _strrev(cripto->frase[i]));
    }

    printf("TEXTO DEPOIS DE PROCESSADO: \n");
    for (int i = 0; i < strlen(cripto->frase); i++)
    {
        printf("%s", cripto->frase[i]);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.