Grazie mille... Un errore di distrazione che non avevo notato, ora il programma funziona benissimo. Fra l'altro, come avevi detto, senza out[i-2]=0 la frase terminava con un simbolo, invece aggiungendolo la stringa si conclude come dovrebbe. Grazie ancora!
Il codice finale
Codice:
/************ CIFRARIO DI CESARE - INPUT DEL TIPO *key**direction(< o >)**parola1*-*parola2*. ************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int caesar_key(char *input)
{
int in, key;
in = input[0];
key = in - 48;
return key;
}
int caesar_direction(char *input)
{
int direction;
if(input[1] == '<')
direction = 0;
else if(input[1] == '>')
direction = 1;
return direction;
}
char *caesar_code(int key, int direction, char *input)
{
char *out;
out = (char*)malloc(sizeof(char[MAX]));
int end, i = 2;
do
{
for(; input[i]!='-' && input[i]!='.'; i++)
{
if(direction==0)
{
if(input[i] >= ('a' + key))
out[i-2] = input[i]-key;
else
out[i-2] = 'z' - ((key-(input[i]-'a'))-1);
}
else if(direction==1)
{
if(input[i] <= ('z' - key))
out[i-2] = input[i] + key;
else
out[i-2] = 'a' + ((key-('z'- input[i]))-1);
}
else
{
printf("\nERRORE - caesar_direction non riconosciuta\n");
return NULL;
}
}
if (input[i] == '-')
{
out[i-2] = ' ';
i++;
end = 0;
}
else if(input[i] == '.')
{
out[i-2] = 0;
return out;
}
}while(end == 0);
}
int main()
{
char *input, *output;
int key, direction;
input = (char*)malloc(sizeof(char[MAX]));
output = (char*)malloc(sizeof(char[MAX]));
printf("Scrivere la parola da tradurre: ");
scanf("%s", input);
key = caesar_key(input);
direction = caesar_direction(input);
output = caesar_code(key, direction, input);
printf("\nChiave: %d", key);
printf("\nDirezione: %d ", direction);
if(direction == 0)
printf("(dx-sx)");
else if(direction == 1)
printf("(sx-dx)");
printf("\nTraduzione: %s", output);
return 0;
}