/* Copyright 2016 Grady Moran Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /* * A simple caesar cipher program to print all 26 character rotations of an input string. */ #include #include #include int main(int argc, char * args[]) { if (argc != 2){ printf("Usage: rot_n \"INPUT_STRING\"\n"); return 1; } int total_len = (strlen(args[1]) + 1); char inputstr[total_len]; strcpy(inputstr, args[1]); char strings[25][total_len]; //array to hold all 25 rotated strings, to be printed later int i, j; //i tracks current rotation number (= 1 less than number rotations to perform this iteration) //j tracks index in input string to perform operation on for (i = 0; i < 25; i++){ for (j = 0; j < total_len; j++){ if (inputstr[j] >= 65 && inputstr[j] <= 90){ //UPPER strings[i][j] = ((inputstr[j] - 'A' + i + 1) % 26) + 'A'; } else if (inputstr[j] >= 97 && inputstr[j] <= 122) { //lower strings[i][j] = ((inputstr[j] - 'a' + i + 1) % 26) + 'a'; } else { //special character strings[i][j] = inputstr[j]; } } strings[i][strlen(inputstr)] = '\0'; //must terminate our strings } printf("%s\n", inputstr); //makes output easier to read if input string printed again for (i = 0; i < 25; i++){ //print them all printf("%s\n", strings[i]); } return 0; }