1
I’m writing an algorithm that encrypts text using a word as a password, Vigenère cipher. Upper and lower case characters should encrypt, special characters and numbers should be ignored. My questions:
- When running the program, enter the password and later the text, error occurs Segmentation failure (recorded core image). For what?
- The code is not working, how to improve it?
code:
#include<cc50.h> // BIBLIOTECA DO CURSO QUE ESTOU FAZENDO.
#include<string.h>
#include<stdio.h>
int
main(int argc, char argv[])
{
if (argc != 2)
{
printf("Erro 1. Digite uma palavra na linha de comando.\n");
return 1;
}
printf("Texto a ser criptografado:\n");
string texto = GetString();
int k = 0;
int l = strlen(texto);
int m = strlen(argv);
for (int i = 0, j = 0; i < l; i++)
{
k = atoi(argv[j]);
if (j > m) // SE O CONTADOR J FOR MAIOR QUE A QUANTIDADE DE CARACTERES DA SENHA, REDEFINE J e K PARA 0.
{
j = 0;
k = 0;
}
else if (texto[i] >= 65 && texto[i] <= 90)
{
texto[i] = (((texto[i] - 65) + k) % 26) + 65;
j++;
}
else if (texto[i] >= 97 && texto[i] <= 122)
{
texto[i] = (((texto[i] - 97) + k) % 26) + 97;
j++;
}
else;
printf("%c", texto[i]);
}
printf("\n");
return 0;
}
It doesn’t have to be a great answer, small tips that guide me the solution serves, because I’m already a few weeks stuck to this problem.
Can it be more specific which error ? And in which line the same happens
– Dev
What is the purpose of this line
k = atoi(argv[j]);
?argv
supposedly only has a string in addition to the executable name, so it quickly fails there if the Indice passes the1
. Not to mention thatargv
should be declared aschar **argv
orchar *argv[]
– Isac