How to play letter by letter of a sentence in an array?

Asked

Viewed 63 times

0

Example: in the sentence naruto, the letter n vector frase is in position frase[0].

I want to put it on mat_cript position [0][0]. Not only her, the rest of the sentence too.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>

int main()

{
char frase[25];
    char mat_cript[i][j];
    int chave, tamanho_frase, cont,i,j;

    system("cls");

    printf("Digite a frase a ser criptografada : ");
    gets(frase);

    while(chave<1 || chave>25)
    {
        printf("Digite a chave a ser utilizada (<= 25) : ");
        scanf("%d",&chave);
        fflush(stdin);
    }

    tamanho_frase = strlen(frase);
    for(cont=0;cont<tamanho_frase;cont++)
        frase[cont] = frase[cont]+chave;

    printf("\n\nFRASE CRIPTOGRAFADA : %s",frase);
    getchar();

    for(i=0;i < tamanho_frase;i++){
        for(j=0;j< tamanho_frase;j++){
            mat_cript[i][j]=frase[i];
        }
    }

    for(i=0;i < tamanho_frase;i++){
        for(j=0;j< tamanho_frase;j++){
            printf(" %d°elemento da linha %d, coluna %d: %s",j+1;i+1;j+1;mat_cript[i][j]);
        }
    }

}

1 answer

1


To do this, simply take control of the rows of your matrix and insert each character into the matrix separately. You must assign each sentence character in a matrix position.

In this example, the character - in all positions of a matrix, then the number of characters of the sentence was counted and placed each character in a position in the matrix:

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

main(){
    char valid_chars[27];

    char mat_cript[10][10];
    char frase[10];

    srand(time(NULL));

    strcpy(valid_chars, "abcdefghijklmnopqrstuvwxyz");

    strcpy(frase, "naruto");
    int num = strlen(frase);

    for(int i = 0; i < 10; i++){
        for(int j = 0; j < 10; j++){
            int control = (rand() % 26);

            mat_cript[i][j] = valid_chars[control];;
        }
    }
    for(int i = 0; i < num; i++){
        mat_cript[5][i] = frase[i];
    }

    for(int i = 0; i < 10; i++){
        for(int j = 0; j < 10; j++){
            printf("\t%c", mat_cript[i][j]);
        }
        printf("\n");
    }
}

See working on Ideone.

  • Hi Gabriel , as it would look for a sentence of n characters and filling the rest of the indexes with random characters

  • @Roni edited the answer. First it already picks from n characters, which is what it does in strlen(frase);. In the code it arrow random values and then, fill with the phrase you want, according to the position you want, there is your control where it will be inserted.

  • thanks, I’m doing a transposition encryption and honestly ...I don’t know if I’ll be able to.

  • @Roni if the answer helped you, mark it as accepted and try to do yes, any questions, just call

Browser other questions tagged

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