Hello guys, I found this Caesar cipher on the internet, but he had problems, so I edited it and now it works wonders. Sorry if the notes are in Italian, they are not essential. Tell me what you think ;)
Code:
#include "stdafx.h" //solo se si usa Microsoft Visual Studio
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void cesare(char s, int salto)
{
char alfabeto[26] = { 'a', 'b', 'c', 'd' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n', 'o', 'p' ,'q', 'r', 's' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' };
int i;
for (i = 0; i<26; i++)
{
if (s == alfabeto[i])
{
printf("%c", alfabeto[i + salto]);
break;
}
}
}
//main
int main()
{
char cript[100];
int S, lung, i;
printf("Inserisci la parola da criptare > ");
scanf("%s", cript); //se si usa Microsoft Visual Studio bisogna andare su Progetto >> Proprietà di *nome progetto* >> C/C++ >> Avanzate >> Disabilita errori specifici >> inserire 4996
printf("Inserisci il salto da eseguire > ");
scanf("%d", &S);
if (S > 26 || S < 0)
{
while (S > 26 || S < 0)
{
printf("Inserisci il salto da eseguire > ");
scanf("%d", &S);
}
}
lung = strlen(cript);
for (i = 0; i<lung; i++)
{
cesare(cript[i], S);
}
printf("\n");
system("PAUSE");
return 0;
}
- - - Updated - - -
I just did a change:
Code:
void cesare(char s, int salto)
{
char alfabeto[52] = { 'a', 'b', 'c', 'd' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n', 'o', 'p' ,'q', 'r', 's' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'a', 'b', 'c', 'd' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n', 'o', 'p' ,'q', 'r', 's' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' };
int i;
for (i = 0; i<52; i++)
{
if (s == alfabeto[i])
{
printf("%c", alfabeto[i + salto]);
break;
}
}
}