Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- string strtolower(string text);
- string strtoupper(string text);
- bool is_alphabetical(string text);
- int main(int argc, string argv[])
- {
- // if user gives too few or too many args
- if (argc < 2 || argc > 2)
- {
- printf("Usage: ./substitution key\n");
- return 1;
- }
- else
- {
- string key = argv[1];
- int key_length = strlen(key);
- // if user gives a key of incorrect length
- if (key_length < 26 || key_length > 26)
- {
- printf("Key must contain 26 characters.\n");
- }
- else
- {
- string key_upper = strtoupper(key);
- string key_lower = strtolower(key);
- printf("%s\n", key_upper);
- printf("%s\n", key_lower);
- printf("%s\n", strtoupper(key));
- if (is_alphabetical(key))
- {
- printf("is alpha\n");
- }
- else
- {
- printf("is not alpha\n");
- }
- /* check if key contains non-alphabetic characters
- ** or contains a character more than once
- */
- // get message from user
- /* iterate over each char
- ** if is upper, substitute using key_upper
- ** if is lower, substitute using key_lower
- */
- }
- }
- }
- string strtolower(string text)
- {
- int length = strlen(text);
- for (int i = 0; i < length; i++)
- {
- text[i] = tolower(text[i]);
- }
- return text;
- }
- string strtoupper(string text)
- {
- int length = strlen(text);
- for (int i = 0; i < length; i++)
- {
- text[i] = toupper(text[i]);
- }
- return text;
- }
- bool is_alphabetical(string text)
- {
- int length = strlen(text);
- for (int i = 0; i < length; i++)
- {
- if (!isalpha(text[i]))
- {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment