Hello there. This is a small program that reads a line from the screen, takes the ASCII values from the entered text, and edits it. Next, the ASCII values are reintepreted(am i misspelling?) as text, and displayed. However, my final output has (allways?) at least 7 characters, even if you enter only one. I'm allso trying to accomplish, that the part of the string which isn't filled, will be printed empty. In spaces.
compiled with -std=c99.
Code:
#include <stdio.h>
int main(){
int maxsize;
printf("How long is your message?\n");
scanf("%d",&maxsize);
int iinput[maxsize], ioutput[maxsize], key, c;
char cinput[maxsize], coutput[maxsize];
printf("What is the message you want to (de)code?\n");
scanf("%s",cinput);
for(int i=0;i<maxsize;i++){
iinput[i]=(int)cinput[i];
}
printf("Thank you. ");
printf("What is your key?\n");
scanf("%d",&key);
lchoice:
printf("1 for coding, 2 for decoding.\n");
scanf("%d",&c);
switch (c)
{
case 1:
for(int j=0;j<maxsize;j++){
ioutput[j]=iinput[j]+key;
}
for(int k=0;k<maxsize;k++){
coutput[k]=(char)ioutput[k];
}
break;
case 2:
for(int j=0;j<maxsize;j++){
ioutput[j]=iinput[j]-key;
}
for(int k=0;k<maxsize;k++){
coutput[k]=(char)ioutput[k];
}
break;
default:
printf("You did not enter 1 or 2.\n");
goto lchoice;
}
printf("This is the coded message:\n");
printf("%s\n",coutput);
system("pause");
return(0);
}
I would really appreciate some help. Thanks in advance.