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

0

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

I want to put it in mat_cript position [0][0] . Not just 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]);
        }
    }

}
    
asked by anonymous 16.11.2018 / 17:05

1 answer

1

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

In this example, the character - was placed in all positions of an array, then the character quantity of the phrase was counted and each character placed in a position in the array:

#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 at Ideone .

    
16.11.2018 / 18:24