diff options
author | EliteTK <tomasz.kramkowski@gmail.com> | 2014-11-15 23:37:27 +0000 |
---|---|---|
committer | EliteTK <tomasz.kramkowski@gmail.com> | 2014-11-15 23:37:27 +0000 |
commit | a33e82b585dadd520eff011f9597459bcdefa5b2 (patch) | |
tree | 290c667b47f98de15acb308aa98baf3efb94c901 | |
parent | 046c70cd0a4106e9cce9e2faaa65bd71fa01eb15 (diff) | |
download | c-stuff-a33e82b585dadd520eff011f9597459bcdefa5b2.tar.gz c-stuff-a33e82b585dadd520eff011f9597459bcdefa5b2.tar.xz c-stuff-a33e82b585dadd520eff011f9597459bcdefa5b2.zip |
cap.c: new; rot.c: improved
-rw-r--r-- | cap.c | 3 | ||||
-rw-r--r-- | rot.c | 39 |
2 files changed, 23 insertions, 19 deletions
@@ -14,8 +14,9 @@ char *capitalise(char *string) { + size_t length = strlen(string); bool last_sep = true; /* Nothing is a separator too. */ - for (int i = 0; i < strlen(string); i++) { + for (unsigned i = 0; i < length; i++) { if (isalpha(string[i])) { string[i] = last_sep ? toupper(string[i]) : tolower(string[i]); last_sep = false; @@ -13,24 +13,27 @@ #include <ctype.h> #include <stdlib.h> -#define mod(A) ((A)%26) - int main(int argc, char **argv) { - if(argc!=3) - exit(1); - char *string = *(argv+1); - int i, rot = (int)strtol(*(argv+2), NULL, 10); - for(i=0; i<strlen(string); i++) - if(isalpha(string[i])){ - if(string[i]-'A'<26){ // Simple way to differentiate lower and upper case if all letters are known to be alpha. - putchar(mod(string[i]-'A'+rot)+'A'); - }else{ - putchar(mod(string[i]-'a'+rot)+'a'); - } - }else{ - putchar(string[i]); - } - putchar('\n'); - return 0; + if (argc != 3) { + printf("Incorrect number of arguments.\n"); + exit(1); + } + + int rot = strtol(argv[2], NULL, 10); + unsigned length = strlen(argv[1]); + + for (unsigned i = 0; i < length; i++) { + if (isalpha(argv[1][i])) { + if (argv[1][i] - 'A' < 26) + putchar((argv[1][i] - 'A' + rot) % 26 + 'A'); + else + putchar((argv[1][i] - 'a' + rot) % 26 + 'a'); + } else + putchar(argv[1][i]); + } + + putchar('\n'); + + return 0; } |