Guest User

Untitled

a guest
Dec 22nd, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. string strtolower(string text);
  7. string strtoupper(string text);
  8. bool is_alphabetical(string text);
  9.  
  10. int main(int argc, string argv[])
  11. {
  12. // if user gives too few or too many args
  13. if (argc < 2 || argc > 2)
  14. {
  15. printf("Usage: ./substitution key\n");
  16. return 1;
  17. }
  18. else
  19. {
  20. string key = argv[1];
  21. int key_length = strlen(key);
  22. // if user gives a key of incorrect length
  23. if (key_length < 26 || key_length > 26)
  24. {
  25. printf("Key must contain 26 characters.\n");
  26. }
  27. else
  28. {
  29. string key_upper = strtoupper(key);
  30. string key_lower = strtolower(key);
  31. printf("%s\n", key_upper);
  32. printf("%s\n", key_lower);
  33. printf("%s\n", strtoupper(key));
  34. if (is_alphabetical(key))
  35. {
  36. printf("is alpha\n");
  37. }
  38. else
  39. {
  40. printf("is not alpha\n");
  41. }
  42.  
  43. /* check if key contains non-alphabetic characters
  44. ** or contains a character more than once
  45. */
  46.  
  47. // get message from user
  48.  
  49. /* iterate over each char
  50. ** if is upper, substitute using key_upper
  51. ** if is lower, substitute using key_lower
  52. */
  53. }
  54. }
  55. }
  56.  
  57. string strtolower(string text)
  58. {
  59. int length = strlen(text);
  60. for (int i = 0; i < length; i++)
  61. {
  62. text[i] = tolower(text[i]);
  63. }
  64. return text;
  65. }
  66.  
  67. string strtoupper(string text)
  68. {
  69. int length = strlen(text);
  70. for (int i = 0; i < length; i++)
  71. {
  72. text[i] = toupper(text[i]);
  73. }
  74. return text;
  75. }
  76.  
  77. bool is_alphabetical(string text)
  78. {
  79. int length = strlen(text);
  80. for (int i = 0; i < length; i++)
  81. {
  82. if (!isalpha(text[i]))
  83. {
  84. return false;
  85. }
  86. return true;
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment