Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /please like and comment
- #include <stdio.h>
- #include <string.h>
- //Returns the number of letters (alpha characters) in usrStr
- int gNoOfChars = 0;
- int getNumAlpha(const char usrStr[]) {
- /* Type your code here. */
- return strlen(usrStr);
- }
- //Returns character stored in replaceChar if thisChar matches thatChar,
- //otherwise returns character stored in thisChar
- char encodeChar(const char thisChar, const char thatChar, const char replaceChar) {
- /* Type your code here. */
- if (thatChar == thatChar)
- return replaceChar;
- else
- return thisChar;
- }
- //encodes usrStr following the replacement guidelines in the lab assignment description
- void encodeString(char usrStr[]) {
- /* Type your code here. */
- int i = 0;
- char ch;
- int size = strlen(usrStr);
- for (i = 0; i < size; i++)
- {
- ch = usrStr[i];
- if ((ch >= 65 && ch <= 90) || ch >= 97 && ch <= 122)
- gNoOfChars++;
- switch (usrStr[i])
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '#');
- break;
- case 'O':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '0');
- break;
- case 'I':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '1');
- break;
- case 'Z':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '2');
- break;
- case 'E':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '3');
- break;
- case 'H':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '4');
- break;
- case 's':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '5');
- break;
- case 'b':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '6');
- break;
- case 'L':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '7');
- break;
- break;
- case 'B':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '8');
- break;
- case 'g':
- usrStr[i] = encodeChar(usrStr[i], usrStr[i], '9');
- break;
- default:
- break;
- }
- }
- }
- int main(void) {
- /* Type your code here. */
- int i = 0;
- char usrInput[100] = { '\0' };
- int flag = 0;
- int usrInputLen = 0;
- printf("Enter a Message: ");
- i = 0;
- while (i<100 - 1)//read only 100 characters
- {
- scanf("%c", &usrInput[i]);
- if (usrInput[i] == '\n')
- {
- flag = 1;
- break;
- }
- i++;
- }
- if (flag == 0)//out of the loop but new line not found.
- {
- printf("\r\nInput message should be 100 characters long only.");
- return 0;
- }
- printf("\r\nYou Entered: %s", usrInput);
- usrInputLen = getNumAlpha(usrInput);
- printf("\r\nNumber of Characters: %d", usrInputLen);
- encodeString(usrInput);
- printf("\r\n Number of Letters: %d", gNoOfChars);
- printf("\r\nEncoded Message: ");
- for (i = 0; i < usrInputLen; i++)
- printf("%c", usrInput[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement