I made myself vigenere cypher console program in c. My code have many parts which I do understand, but the output is wrong and don't know how to fix it. My goal is to be able to encipher full paragraph.
Lines 5-36 -> the table. Lines 37-51 -> declarations, inputs, notes. Lines 52-52 -> setting up j
for longer input. 59-74 -> comparison inputs with alphabet. 77-81 -> printing out inputs. 82-84 -> length of inputs. 85-892 -> positions in alphabet. 94-121 -> array of array. 122 -> declarations of numbers.. 132-129 -> printing array of array. 130-150 -> my poor encryption.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
printf(" |\n");
printf(" | 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 \n");
printf("---+------------------------------------------------------------------------------ \n");
printf(" A | 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 \n");
printf(" B | 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 \n");
printf(" C | 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 \n");
printf(" D | 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 \n");
printf(" E | 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 \n");
printf(" F | 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 \n");
printf(" G | 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 \n");
printf(" H | 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 \n");
printf(" I | 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 \n");
printf(" J | 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 \n");
printf(" K | 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 \n");
printf(" L | 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 \n");
printf(" M | 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 \n");
printf(" N | 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");
printf(" O | 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 \n");
printf(" P | 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 \n");
printf(" R | 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 \n");
printf(" S | 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 \n");
printf(" T | 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 \n");
printf(" U | 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 \n");
printf(" V | 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 \n");
printf(" W | 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 \n");
printf(" X | 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 \n");
printf(" Y | 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 \n");
printf(" Z | 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 \n");
printf("================================================================================== \n");
printf("USE ONLY THIS : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
printf(" CHARRACTERS : abcdefghijklmnopqrstuvwxyz\n");
printf("================================================================================== \n");
char s1[20],s2[20];
char abeceda[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'};
printf("First input: ");
scanf("%s",&s1);
printf("Secound input: ");
scanf("%s",&s2);
int i=0,k=0,j=0,l=0,m=0,y=0,x=0;
j=strlen(s1),l=strlen(abeceda),m=strlen(s2);
int r=10;
int array[50],array0[50];
printf("\n");
printf("1-means code found agreement with first input.\n");
printf("2-means code found agreement with secound input.\n");
printf("\n");
printf("number of matches:\n");
if(strlen(s1)<=strlen(s2))
{
j=strlen(s2);
}
else{
j=(strlen(s1));
}
for(i=0;i<j;i++){
for(k=0;k<l;k++){
//first input
if(s1[i]==abeceda[k]){
printf("1");
//printf("%c ",s1[i]);//printf("%i",k);
array[y]=k;
y++;
}
//druhe slovo
if(s2[i]==abeceda[k]){
printf("2");
//printf(" | ");//printf("%c\n",s2[i]); //printf("%i",k);
array0[x]=k;
x++;
}}}
printf("\n");
printf("\n");
printf("-1-|-2-\n");
for(i=0;i<j;i++){
/*for(k=0;k<l;k++){if(s1[i]!=abeceda[k]){ s1[i]=" ";}if(s2[i]!=abeceda[k]){ s2[i]=" ";printf("1");} printf("\n"); }*/
printf(" %c | %c\n",s1[i],s2[i]);
}
printf("\nlen of s1: %i",y);
printf("\nlen of s2: %i\n",x);
printf("\n");
for(k=0;k<j;k++){
printf("%i,",array[k]);
}
printf("\n");
for(k=0;k<m;k++){
printf("%i,",array0[k]);
}
printf("\n\n");
printf("comparing table:\n");
char Row_Of_Row[26][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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
{'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'},
};
int a=0,b=25,c=0,d=0,e=0;
for(a=0;a<=b;a++){
for(c=0;c<=b;c++){
printf("%c",Row_Of_Row[c][a]);
}
printf("\n");
}
printf("\n");
char NewRoad[j];
printf("Length of output word is: %i",j);
printf("\n");
printf("encryption: ");
for(a=0;a<j;a++){
d=array[a];
e=array0[a];
printf("%i,%i\n",d,e);
printf("%c",Row_Of_Row[d][e]);
//NewRoad[a]=Row_Of_Row[d][e];
//printf("%c",NewRoad[a]);
NewRoad[a]=Row_Of_Row[d][e];
printf("\n");
}
printf("Output is: ");
for(a=0;a<j;a++){
printf("%c",NewRoad[a]);
}
printf("\n%s",NewRoad);
}
Know its ugly but I'm stuck for days. My code has different output depending on size of inputs. I think that is because of compiler but not sure. When I try Visual Studio Code output was also different.
Dev c++ Windows 7:
- input: wwww
- input: xxxx
output: "tyza"
Dev c++ Windows 10:
- input: wwww
- input: xxxx
output: "tttt"
This is okay but when I tried longer inputs this happened.
- input: 35*"w"
- input: 35*"x"
output: "uuutttttttttttttttttttttttttttttttz"
Another issue is:
- input: w
- input: b
output: "xřb"
Any ideas why this is happening and how do I fix it? Sorry for mistakes, I'm a beginner.