Extraños no deseados tres copias impresas de código de cifrado César dígitos

StackOverflow https://stackoverflow.com/questions/3838238

  •  27-09-2019
  •  | 
  •  

Pregunta

El código de cifrado funciona realmente; es sólo que tengo algunos códigos de tres dígitos impares separados por barras también. Cualquier ayuda sería muy apreciada. Aquí está mi código.

Los códigos de este aspecto, pero tienen números aleatorios / 354/233/645/810/236

    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "cs50.h"

    int i, len;
    string sentance, encrypted;
    int k, argvLen;

    int caesar (int k){

 printf("Hi I'm Ceaser! What would you like me to cipher?\n");

 sentance = GetString();
 len = strlen(sentance);
 char encrypted[len];

     for (i=0; i<len; i++) {
 if (sentance[i] >='a' && sentance[i] <='z') {
  encrypted[i] = ((sentance[i] - 'a' + k) % 26) + 'a';

  }
 else if (sentance[i] >='A' && sentance[i] <='Z') {
  encrypted[i] = ((sentance[i] - 'A' + k) % 26) + 'A';

     }
 else if (sentance[i] >=' ' && sentance[i] <= '@'){
  encrypted[i] = sentance[i];
 }

 }


 printf("%s", encrypted);
 return 0;
    };


    int main (int argc, const char * argv[]) {

 if (argc==2) {
  k = atoi(argv[1]);
  argvLen = strlen(argv[1]);
  for (i=0; i<argvLen; i++){
   if (isdigit(argv[1][i])){   
   caesar(k);
      }
   else {
   printf("please enter a number for the key!");
   return 1;
   }

     }

    return 0;

 }
    };
¿Fue útil?

Solución

Usted no se da por concluida la cadena encrypted correctamente.

Necesita:

  1. Para asegurarse de que tiene espacio para el carácter de terminación, mediante el uso de char encrypted[len + 1];.
  2. encrypted[len] = '\0'; después del bucle.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top