I have an assignment to create a program that converts each character in a string by a set number entered at the command line. Example: if the user enters 1 at the command line then enters abc, def then the program should convert the string to bcd, efg.
I've written the program but I can't figure out how to get the program to not convert the punctuation characters.
The program currently converts abc, def and prints bcdefg. It needs to print bcd, efg and include the punctuation characters without converting them.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[]) //user enter number at cmd prompt
{
string key = argv[1]; //store user entered number
int k = atoi(argv[1]); //only accept consecutive digits
if (argc != 2 || k == 0)
{
printf("Usage: ./caesar key\n"); /*show error if user enters non-consecutive digits*/
return 1;
}
string original = get_string("Plaintext: "); /* prompt user for message to spin*/
for (int i = 0, n = strlen(original); i < n; i++) /* get string length and loop char change*/
if (isalnum(original[i])) /* only convert alphanumeric character*/
printf("%c", original[i] + k); /* print and convert character by number entered at prompt*/
printf("\n");
return 0;
}