Question

****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of...

****C PROGRAMMING****

(100 points) Write a program that prompts the user to enter the name of a file. The program encoded sentences in the file and writes the encoded sentences to the output file.

Enter the file name: messages.txt

Output: encoded words are written to file: messages.txt.swt

The program reads the content of the file and encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i, with the letter with alphabetical position 25 – i. For example, letter a is at position 0 and after encoding it becomes letter z (position 25). Letter m is at position 12, it becomes letter n (position 13) after encoding because 25 – 12 is 13.

1. Name your program file_encode.c. The output file name should be the same name as the input file but with an added extension of .swt. For example, if the original file name is abc.txt, the output file name is then abc.txt.swt. Assume the file name is no more than 100 characters. Assume the length of each line in the input file is no more than 100 characters. Assume the input file contains no more than 1000 lines.

2. In the program, you should include the following function. void line_switch(char *s1, char *s2);

The function expects s1 to point to a string containing the input as a string and stores the output to the string pointed by s2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include <stdio.h>
#include <ctype.h>
#define WORD_LEN 100

int read_line(char *str, int n);

void line_switch(char *message, char *shift_message);

int main()
{
    int shift_amount;
    char sen1[WORD_LEN+1];
    char sen2[WORD_LEN+1];
    FILE *fp,*fp1;
    char word[100],ch;
    fp = fopen("messages.txt","r");
    fp1 = fopen("messages.txt.swt","w+");
    if ( fp )
   {
    while(!feof(fp))//this loop searches the for the current word
{
    fscanf(fp,"%s",sen1);
    line_switch(sen1, sen2);
    fprintf(fp,"%s",sen2);

}
   }


    else
      {
         printf("Failed to open the file\n");
    }
   fclose(fp);
     fclose(fp1);

    return 0;
}

void line_switch(char *message, char *shift_message)
{


    for(; *message != '\0'; message++)
    {

             if(*message >= 'a' && *message <= 'z')
             *shift_message++ = (25-(*message - 'a') )% 26 + 'a';
          else if(*message >= 'A' && *message <= 'Z')
             *shift_message++ = (25-(*message - 'A' ))% 26 + 'A';
          else
            *shift_message++ = *message;
    }

    *shift_message='\0';
}

Add a comment
Know the answer?
Add Answer to:
****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • 14.3: More Sentences Write a program that allows a user to enter a sentence and then...

    14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...

  • Write a program that separately prompts the user for a first name and last name and...

    Write a program that separately prompts the user for a first name and last name and outputs a string containing the following information, in order: a. First letter of the user's name. b. First five letters of the user's last name. c. A random two-digit integer You must construct the desired string ensuring all characters are lowercase; output the identification string accordingly. Assume the last name contains at least 5 characters. You must use the Random (java.util.Random) class to generate...

  • C programming 1 String Merging Write a program that reads two strings s1 and s2 from...

    C programming 1 String Merging Write a program that reads two strings s1 and s2 from the keyboard and merges them to a string s3. Strings s1 and s2 are statically allocated whereas s3 is dynamically allocated to fit exactly s1 and s2. The two original strings are no longer than 100 characters long. Use the following function to merge the two strings. char *stringMerge(char s1[], char s2 []); Example: Enter the first string: Hello world Enter the first string:...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • Write the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT