Question

In: Computer Science

Assignment in C: prompt the user to enter secret message that is terminated by presding Enter....

Assignment in C:
prompt the user to enter secret message that is terminated by presding Enter. You can assume that the the length of this message will be less than 100 characters. You will then parae this message, character by character, converting them to lower case, and find corresponding characters in the words found in the key text word array. Once a character match is found, you will write the index of the word and the index of the character to a character string that you will later write out to the text file. Spaces are to be placed into the text as found in the message and will be used to delimit the separate words in the secret message. Once the message has been encoded, prompt the user for the name of a file to save the encoded message to, and save it to that file

Solutions

Expert Solution

Program Code Screenshot



Program Sample Console Input/Output Screenshot

out.txt after execution


Program Code to Copy

#include <stdlib.h>
#include <stdio.h>



int main(int argc, char *argv[])
{
    // create a key text work array for encryption
    // we can add more characters in this for encoding
    const int length=6;
    char key_text[6][10]={"qwe", "rtyui", "opasd", "fghjkl", "zxcv", "bnm"};

    // prompt the user to enter secret message that is terminated by presding Enter.
    printf("Enter Secret Message: ");

    // can assume that the the length of this message will be less than 100 characters
    char buffer[100];

    //take input
    scanf("%[^\t\n]s",buffer); 

    // declare variable to store encrypted message and its counter
    char message[200];
    int k=0;

    // parse the message character by character
    int pos=0;
    while(buffer[pos]!='\0'){
        char x = buffer[pos];

        // check for space
        // Spaces are to be placed into the text as found in the message and will be used to delimit the separate words in the secret message
        if(x==' '){
            message[k++]=' ';
            pos++;
            continue;
        }

        int found=0;//variable to check if character if found

        int i=0, j=0; //indexes to be traversed

        // search this character in key text array
        for(i=0;i<length;i++){
            j=0;
            while(key_text[i][j]!='\0' && key_text[i][j]!=x){
                j++;
            }
            if(key_text[i][j]==x){
                found=1;
                break;
            }
        }
        if(found==1){
            // add index of word and character in the message
            char str[100];
            itoa(i, str, 100);
            int cp =0;
            while(str[cp!='\0']){
                message[k++]=str[cp++];
            }
            itoa(j, str, 100);
            cp =0;
            while(str[cp!='\0']){
                message[k++]=str[cp++];
            }
        }else{
            printf("character %c not found in key_text array\n", x);
        }
        pos++;
    }
    message[k]='\0';

    // Once the message has been encoded, prompt the user for the name of a file 
    char fileName[100];
    printf("Enter file to save encoded message: ");
    scanf("%s", fileName);

    //save encoded message to this file
    FILE *fp;
    fp = fopen(fileName, "w+");
    fprintf(fp, "%s", message);
    fclose(fp);
}

Related Solutions

IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
DO IN C++ Secret Codes! Create a program that will accept a message from the user...
DO IN C++ Secret Codes! Create a program that will accept a message from the user and either encrypt ordecrypt it with the following algorithms: To encrypt: - get the character you wish to encrypt - find the index of that character in the alphabet array - add the shift offset to the index - add the increment to the index - "wrap around" the new index so the result is between 0 and 29 - find the character at...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the week and display the full name of the day of the week. Use an enumerated data type to solve this problem. Enumerate the days of the week in a data type. Start with Monday and end with Friday. Set all of the characters of the user input to lower case. Set an enumerated value based on the user input. Create a function that displays...
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code please (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
Program IN C Prompt the user to enter month number and year. Retain an appropriate response...
Program IN C Prompt the user to enter month number and year. Retain an appropriate response for invalid month numbers, but don’t worry about validity of year. 2.Determine the number of days in the month entered (ignore leap years). 3.Use the answer to step 2 in one FOR-LOOP to ask the user to enter a FAHRENHEIT temperature (integer or floating) for each day of the month utilizing a prompt that includes the DATE. a. Prompt the user to enter the...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT