Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. /please like and comment
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <string.h>
  6.  
  7. //Returns the number of letters (alpha characters) in usrStr
  8.  
  9. int gNoOfChars = 0;
  10.  
  11. int getNumAlpha(const char usrStr[]) {
  12.  
  13. /* Type your code here. */
  14.  
  15. return strlen(usrStr);
  16.  
  17. }
  18.  
  19. //Returns character stored in replaceChar if thisChar matches thatChar,
  20.  
  21. //otherwise returns character stored in thisChar
  22.  
  23. char encodeChar(const char thisChar, const char thatChar, const char replaceChar) {
  24.  
  25. /* Type your code here. */
  26.  
  27. if (thatChar == thatChar)
  28.  
  29. return replaceChar;
  30.  
  31. else
  32.  
  33. return thisChar;
  34.  
  35. }
  36.  
  37. //encodes usrStr following the replacement guidelines in the lab assignment description
  38.  
  39. void encodeString(char usrStr[]) {
  40.  
  41. /* Type your code here. */
  42.  
  43. int i = 0;
  44.  
  45. char ch;
  46.  
  47. int size = strlen(usrStr);
  48.  
  49. for (i = 0; i < size; i++)
  50.  
  51. {
  52.  
  53. ch = usrStr[i];
  54.  
  55. if ((ch >= 65 && ch <= 90) || ch >= 97 && ch <= 122)
  56.  
  57. gNoOfChars++;
  58.  
  59. switch (usrStr[i])
  60.  
  61. {
  62.  
  63. case '0':
  64.  
  65. case '1':
  66.  
  67. case '2':
  68.  
  69. case '3':
  70.  
  71. case '4':
  72.  
  73. case '5':
  74.  
  75. case '6':
  76.  
  77. case '7':
  78.  
  79. case '8':
  80.  
  81. case '9':
  82.  
  83. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '#');
  84.  
  85. break;
  86.  
  87. case 'O':
  88.  
  89. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '0');
  90.  
  91. break;
  92.  
  93. case 'I':
  94.  
  95. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '1');
  96.  
  97. break;
  98.  
  99. case 'Z':
  100.  
  101. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '2');
  102.  
  103. break;
  104.  
  105. case 'E':
  106.  
  107. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '3');
  108.  
  109. break;
  110.  
  111. case 'H':
  112.  
  113. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '4');
  114.  
  115. break;
  116.  
  117. case 's':
  118.  
  119. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '5');
  120.  
  121. break;
  122.  
  123. case 'b':
  124.  
  125. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '6');
  126.  
  127. break;
  128.  
  129. case 'L':
  130.  
  131. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '7');
  132.  
  133. break;
  134.  
  135. break;
  136.  
  137. case 'B':
  138.  
  139. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '8');
  140.  
  141. break;
  142.  
  143. case 'g':
  144.  
  145. usrStr[i] = encodeChar(usrStr[i], usrStr[i], '9');
  146.  
  147. break;
  148.  
  149. default:
  150.  
  151. break;
  152.  
  153. }
  154.  
  155. }
  156.  
  157. }
  158.  
  159.  
  160.  
  161. int main(void) {
  162.  
  163. /* Type your code here. */
  164.  
  165. int i = 0;
  166.  
  167. char usrInput[100] = { '\0' };
  168.  
  169. int flag = 0;
  170.  
  171. int usrInputLen = 0;
  172.  
  173. printf("Enter a Message: ");
  174.  
  175.  
  176.  
  177. i = 0;
  178.  
  179. while (i<100 - 1)//read only 100 characters
  180.  
  181. {
  182.  
  183. scanf("%c", &usrInput[i]);
  184.  
  185. if (usrInput[i] == '\n')
  186.  
  187. {
  188.  
  189. flag = 1;
  190.  
  191. break;
  192.  
  193. }
  194.  
  195. i++;
  196.  
  197. }
  198.  
  199. if (flag == 0)//out of the loop but new line not found.
  200.  
  201. {
  202.  
  203. printf("\r\nInput message should be 100 characters long only.");
  204.  
  205. return 0;
  206.  
  207. }
  208.  
  209. printf("\r\nYou Entered: %s", usrInput);
  210.  
  211.  
  212.  
  213. usrInputLen = getNumAlpha(usrInput);
  214.  
  215. printf("\r\nNumber of Characters: %d", usrInputLen);
  216.  
  217. encodeString(usrInput);
  218.  
  219. printf("\r\n Number of Letters: %d", gNoOfChars);
  220.  
  221. printf("\r\nEncoded Message: ");
  222.  
  223. for (i = 0; i < usrInputLen; i++)
  224.  
  225. printf("%c", usrInput[i]);
  226.  
  227. return 0;
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement