I have written this simple program for an assignment, but when I input my text, the output gives me symbols instead of chars. any help would be appreciated. I do not know why my output appears that way, but the program seems to compile fine. Maybe it is working and I need to just do a base test with the math to see if it is functioning properly. In any event if anyone sees errors in this, feedback is much appreciated.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
string Crypto(string, int); // rotation
int main(int argc, string argv[])
{
int k = 0;
// error checing
if (argc == 0 || argc == 1 || argc > 2)
{
// get mad
printf("Enter 1 integer as an argument. Stop messing around!\n Try Again: ");
return 1;
}
else
{
//create command line arguments to be stored into k
k = atoi(argv[1]);
k = k * 1;
}
// Get text to be encrypted
printf("Enter the text you want to encrypt: \n");
string a = GetString();
string b = Crypto(a, k);
printf("%s\n", b);
return 0;
}
//Now let's get cryptic
string
Crypto(string a, int k)
{
int c = 0;
for (int i = 0, n = strlen(a); i < n; i++)
{
if(a[i] >= 65 && a[i] <= 90)
{
c = ((26 - (91 - a[i] + k) % 26));
a[i] = c + 'A';
}
else
{
c = ((26 - (123 - a[i] + k % 26)));
a[i] = c + 'a';
}
}
return a;
}
In function Crypto
, you need to attach a \0
(null) character to signify the end of the string. Just before return a;
, write a a[i+1] = '\0';
statement.