Task - Encode a string (C Language) Modify Project #2, Task #1 (given below) so that input characters are command line arguments. Requirements Name your program project5_encode.c. Input characters are command line arguments. There can be any number of command line arguments. Assume the total number of input characters is no more than 1000. Character handling library functions in ctype.h are allowed. The program should include the following function: void encode(char *input, char *output); The function expects input to point to a string containing the string to be encoded, output to point to a string containing the result. The program should also check if the number of arguments on the command line is greater than or equal to 2. If the number of arguments is 1, the program should display the message "Invalid input!".  Examples (your program must follow this format precisely) Example #1 $ ./a.out 7 + 8 Output: 3_4 Example #2 $ ./a.out usf.edu Output: ayl_kja Example #3 $ ./a.out Invalid input! Project #2, Task #1 Task - Encode a string (C Languege) Write a program that encodes the input characters using the following algorithm. if the input character is a digit ('0' through '9'), then output the digit by adding 6 to the digit and calculate the remainder by 10. For example, if a digit is '6', then replaced digit is '2'. Subtract the digit with '0', add 6, and then calculate the remainder by 10, then add '0'. If the character is an alphabetic letter, then shift the letter by 6. For example, if the letter is ‘B’, then the letter becomes 'H'. Subtract the letter with 'a' (for lower case) or 'A' (for uppder case), add 6, and then calculate the remainder by 26, then add 'a' (for lower case) or 'A' (for uppder case). For example, 'X' shifted by 6 is 'D'. If the character is a white space, skip it. If the character is not a digit, an alphabetic letter, or a white space, output the underscore '_' character.  Requirements Follow the format of the examples below. Use getchar() function to read in the input. Do not use scanf. The user input ends with the user pressing the enter key (a new line character). Character handling library functions in ctype.h are allowed. Examples (your program must follow this format precisely) Example #1 Enter input: 7 + 8 3_4 Example #2 Enter input: usf.edu ayl_kja my answer: #include #include int main()  {     //create a character     char num;     //print enter input for the user to enter their response     printf("Enter input: ");     while ((num = getchar()) != '\n')      {         //if inputs are numbers digit print add 6 to it and calculate remainder by 10.         if (isdigit(num))          {             printf("%c",(num-'0'+6)%10+'0');         }             //if the inputs are letters then shift to the 6th letter from it and calculate the remainder by 26.             else if (isalpha(num))             {                 //if input is in lower case letters then print the output in lower case, else if it is in upper case letters then print output in upper case.                 if (tolower(num))                  {                     printf("%c",(num-'a'+6)%26+'a');                 }                      else                      {                         printf("%c", (num-'A'+6)%26+'A');                     }             }             //if character is white space skip else print '_'             else if (isspace(num))              {                 continue;             }              else              {                 printf("_");             }     }     return 0; }

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.1: Function And Parameter Declarations
Problem 11E
icon
Related questions
Question

Task - Encode a string (C Language)

Modify Project #2, Task #1 (given below) so that input characters are command line arguments.

Requirements

  • Name your program project5_encode.c.
  • Input characters are command line arguments. There can be any number of command line arguments.
  • Assume the total number of input characters is no more than 1000.
  • Character handling library functions in ctype.h are allowed.
  • The program should include the following function:

void encode(char *input, char *output);

    • The function expects input to point to a string containing the string to be encoded, output to point to a string containing the result.
  • The program should also check if the number of arguments on the command line is greater than or equal to 2. If the number of arguments is 1, the program should display the message "Invalid input!"

Examples (your program must follow this format precisely)

Example #1

$ ./a.out 7 + 8
Output: 3_4

Example #2

$ ./a.out usf.edu
Output: ayl_kja

Example #3

$ ./a.out
Invalid input!

Project #2, Task #1

Task - Encode a string (C Languege)

Write a program that encodes the input characters using the following algorithm.

  • if the input character is a digit ('0' through '9'), then output the digit by adding 6 to the digit and calculate the remainder by 10. For example, if a digit is '6', then replaced digit is '2'.
    • Subtract the digit with '0', add 6, and then calculate the remainder by 10, then add '0'.
  • If the character is an alphabetic letter, then shift the letter by 6. For example, if the letter is ‘B’, then the letter becomes 'H'.
    • Subtract the letter with 'a' (for lower case) or 'A' (for uppder case), add 6, and then calculate the remainder by 26, then add 'a' (for lower case) or 'A' (for uppder case). For example, 'X' shifted by 6 is 'D'.
  • If the character is a white space, skip it.
  • If the character is not a digit, an alphabetic letter, or a white space, output the underscore '_' character. 

Requirements

  1. Follow the format of the examples below.
  2. Use getchar() function to read in the input. Do not use scanf.
  3. The user input ends with the user pressing the enter key (a new line character).
  4. Character handling library functions in ctype.h are allowed.

Examples (your program must follow this format precisely)

Example #1

Enter input: 7 + 8
3_4

Example #2

Enter input: usf.edu
ayl_kja

my answer:

#include <stdio.h>
#include <ctype.h>

int main() 
{
    //create a character
    char num;
    //print enter input for the user to enter their response
    printf("Enter input: ");
    while ((num = getchar()) != '\n') 
    {
        //if inputs are numbers digit print add 6 to it and calculate remainder by 10.
        if (isdigit(num)) 
        {
            printf("%c",(num-'0'+6)%10+'0');
        }
            //if the inputs are letters then shift to the 6th letter from it and calculate the remainder by 26.
            else if (isalpha(num))
            {
                //if input is in lower case letters then print the output in lower case, else if it is in upper case letters then print output in upper case.
                if (tolower(num)) 
                {
                    printf("%c",(num-'a'+6)%26+'a');
                } 
                    else 
                    {
                        printf("%c", (num-'A'+6)%26+'A');
                    }
            }
            //if character is white space skip else print '_'
            else if (isspace(num)) 
            {
                continue;
            } 
            else 
            {
                printf("_");
            }
    }
    return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps with 6 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning