#include <stdio.h>
#include <string.h>
int main()
{
int key;
char line[500];
char ch = '\0';
int i = 0;
printf("Enter key: ");
scanf("%d", &key);
getchar();
printf("Enter line: ");
while(ch != '\n')
{
ch = getchar();
line[i] = ch;
i++;
}
int length = i;
int k = 0;
char temp[length];
for(i = 0; i < length; i++)
{
if((int)line[i] >= 65 && (int)line[i] <= 90)
{
temp[k++] = (int)line[i] + key;
} else if((int)line[i] >= 97 && (int)line[i] <= 122){
temp[k++] = (int)line[i] + key;
} else {
temp[k++] = line[i];
}
}
printf("\n");
for(i = 0; i < length; i++)
{
printf("%c", temp[i]);
}
}
What I have done so far is to convert characters into another one. For example, if I input "abc ABC" and the adding value(int key) is 3, then it should print out "def DEF".
This works fine, but what I want to achieve further is to rewind the character if it`s out of bound. For example, if I input "XYZ" and the adding value is 3, it should print out "ABC", not "[]". Can I please get any help or an advice of how to solve this problem? Thanks..