I have segmentation fault somewhere, with gdb i found that it gives the fault at return a inside cypher() function, can i fix it?:
/*
caesar.c
Carlos Lopez J.
luchillo17@gmail.com
This program encrypts a text in Caesar cypher.
*/
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char cypher(char c, int k);
char toAscii(int n);
int main ( int argc, string argv[])
{
if (argc !=2){
printf("Hell no, you need only one comand line argument.\n");
return 1;
}
int k = atoi(argv[1]);
string s1="Be sure to drink your Ovaltine!";
for(int i = 0, n = strlen(s1); i < n; ++i){
s1[i]=cypher(s1[i] , k);
}
printf("Text: %s\n", s1);
printf("Distancia wrap: %c\n", 'A'+25);
return 0;
}
char cypher(char c, int k){
char a;
if(isupper(c)){
a=(((c - 'A')+k)%26)+'A';
printf("%c",(((c - 'A')+k)%26)+'A');
} else if(islower(c)){
a=(((c - 'a')+k)%26)+'a';
printf("%c",(((c - 'a')+k)%26)+'a');
} else {
a=c;
printf("%c",c);
}
return a;
}
char toAscii(int n){
char s[('z'-'a')+('Z'-'A')+2];
for(int i = 0, tam = ('z'-'a')+('Z'-'A')+2; i < tam; ++i){
if (i < 'Z'-'A'+1){
s[i]='A'+i;
} else {
s[i]='a'+i-tam/2;
}
}
printf("ToAscii: %c\n", s[n]);
return s[n];
}