Question

Programming in C.

Name this program schwifty.c - This program reads a text file and makes it schwifty, but the user determines the schwiftiness. The user supplies the filename to schwift and a string containing a sequence of the following characters to determine the schwiftiness via command line arguments:

  • L - Left shift each character in a word:
    • hello --> elloh
  • R - Right shift each character in a word:
    • elloh --> hello
  • I - Shift the letters' and digits' value "up" by one (wraps if necessary) and keeps other characters as the same:
    • a --> b
    • Y --> Z
    • 2 --> 3
    • z --> a (wrap)
    • 9 --> 0 (wrap)
    • [ --> [ (same: not letter or digit)
  • D - Shift the letters' and digits' value "down" by one (wraps if necessary) and leaves other characters as the same:
    • b --> a
    • Z --> Y
    • 3 --> 2
    • a --> z (wrap)
    • 0 --> 9 (wrap)
    • [ --> [ (same - not letter or digit)

Example execution

ex.txt ./a.out ex.txt R ./a.out ex.txt D ./a.out ex.txt RD ./a.out ex.txt RDIL
Oh, yeah! You gotta get schwifty.
empty line
,Oh
!yeah
uYo
agott
tge
.schwifty
empty line
Ng,
xdzg!
Xnt
fnssz
fds
rbgvhesx.
empty line
,Ng
!xdzg
tXn
zfnss
sfd
.rbgvhesx
empty line
Oh,
yeah!
You
gotta
get
schwifty.
empty line

Notes

  • A word is a group of characters surrounded by whitespace.
  • You can assume no word's length will be greater than 100.
  • Each schwiftified word is written to stdout on a newline as shown in the examples.
  • Implement the following four functions in your program that each performs the associated schwift. Your functions will be unit tested, so implement the signatures exactly as shown. A starting code template is downloadable below.
    1. void left(char word[]);
    2. void right(char word[]);
    3. void inc(char word[]);
    4. void dec(char word[]);
  • Error messages:
    1. If there is not exactly 3 command line arguments, then print out the following error message: "Invalid number of arguments".
    2. If the input file cannot be opened, then print out the following error message: "Could not open file 'filename'" where filename is the name of the file (surrounded by single quotes).
    3. If one of the schwifties is invalid (not L, R, I, or D), then print out the following error message and do not print any words: "You threw off my schwiftiness with schwifty X!" where X is the leftmost invalid schwifty encountered in argv[2].
    • If errors 2. and 3. both occur at the same time, only print 2.'s error message.schwifty.c Welcome C schwifty.c X ... WS } Users > lucasdavis > Downloads > C schwifty.c 1 #include <stdio.h> 2 #include <str
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

-------------------schwifty.c-------------------

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

void left(char word[]); //declarations
void right(char word[]);
void inc(char word[]);
void dec(char word[]);

int main(int argc, char** argv)
{
   if (argc !=3){
       printf("Invalid number of arguments\n"); //check for number of arguments
       return 0;
   }

   FILE *fin ;
   fin = fopen(argv[1], "r") ; //file opening failed
   if(fin == NULL){
       printf("Could not open file '%s'\n", argv[1]);
       return 0;      
   }

   int i = 0;
   for(i = 0; i< strlen(argv[2]); i++){
       if(argv[2][i]!='L' && argv[2][i]!='R' && argv[2][i]!='I' && argv[2][i]!='D') { //if wrong char in input
           printf("You threw off my schwiftiness with schwifty %c!\n", argv[2][i]);
           return 0;
       }
   }

   char text[1001];
   while(fgets(text, 1000, fin)) //read line by line
   {
       char* word = strtok(text, " \n"); //split each word from the line sparated by newline or space

       while(word!=NULL) //if word is not empty
       {
           //printf("%s\n", word);
           for(i = 0; i< strlen(argv[2]); i++) //perform operations
               {
                   if(argv[2][i]=='L')
                   {
                       left(word);
                   }
                   if(argv[2][i]=='R')
                   {
                       right(word);
                   }
                   if(argv[2][i]=='I')
                   {
                       inc(word);
                   }
                   if(argv[2][i]=='D')
                   {
                       dec(word);
                   }
               }
           printf("%s\n", word); //print output for that word after all operations are completed
           word = strtok(NULL, " \n"); //find next word from the line
       }

   }

   return 0;
}

void left(char word[]) //shifts left
{
   int i;
   char output[100]; //create output array
   int out_i = 0;
   for(i = 1; i < strlen(word); i++){ //second char to last
       output[out_i++] = word[i];
   }
   output[out_i++] = word[0]; //first char
   output[out_i] = '\0'; //place null char at the end
   strcpy(word, output); //update word with output array value
}

void right(char word[])
{
   char output[100];
   int out_i = 0;
   output[out_i++] = word[strlen(word)-1]; //last char
   int i;
   for(i = 0; i < strlen(word)-1; i++){
       output[out_i++] = word[i]; //1st chat to last but one char
   }
   output[out_i] = '\0';
   strcpy(word, output);
}

void inc(char word[])
{
   char output[100];
   int out_i = 0;
   int i;
   for(i =0; i < strlen(word); i++){
       if(word[i] == 'Z') //special cases
           output[out_i++] = 'A';
       else if(word[i] == 'z')
           output[out_i++] = 'a';
       else if(word[i] == '9')
           output[out_i++] = '0';
       else if(word[i] >= 'A' && word[i] <= 'Y') //A-Y char
           output[out_i++] = word[i]+1;
       else if(word[i] >= 'a' && word[i] <= 'y') //a-y char
           output[out_i++] = word[i]+1;
       else if(word[i] >= '0' && word[i] <= '8') //0-8 char
           output[out_i++] = word[i]+1;
       else
           output[out_i++] = word[i]; //remaining chars are not altered
   }
   output[out_i] = '\0';
   strcpy(word, output);
}

void dec(char word[])
{
   char output[100];
   int out_i = 0;
   int i;
   for(i =0; i < strlen(word); i++){
       if(word[i] == 'A') //special cases
           output[out_i++] = 'Z';
       else if(word[i] == 'a')
           output[out_i++] = 'z';
       else if(word[i] == '0')
           output[out_i++] = '9';
       else if(word[i] >= 'B' && word[i] <= 'Z') //B-Y
           output[out_i++] = word[i]-1;
       else if(word[i] >= 'b' && word[i] <= 'z') //b-y
           output[out_i++] = word[i]-1;
       else if(word[i] >= '1' && word[i] <= '9') //1-9
           output[out_i++] = word[i]-1;
       else
           output[out_i++] = word[i]; //remaining chars are not altered
   }
   output[out_i] = '\0';
   strcpy(word, output);
}

-------------------Screenshot schwifty.c-------------------

-/c/schwifty/schwifty.c - Sublime Text (UNREGISTERED) 11:45 PM schwifty.c ex.txt Х #include<stdio.h> #include<stdlib.h> #incl-/c/schwifty/schwifty.c - Sublime Text (UNREGISTERED) 11:45 PM ex.txt schwifty.c 49 50 f(argv[2][i]==I) } 54 inc (word); if-/c/schwifty/schwifty.c - Sublime Text (UNREGISTERED) 11:45 PM ex.txt х schwifty.c int out i = 0; int i; for(i=0; i < strlen(

-------------------Output-------------------

-/c/schwifty/ex.txt - Sublime Text (UNREGISTERED) 11:45 PM ex.txt X schwifty.c Oh, yeah! You gotta get schwifty. 2 Line 2, Co

ti ) 11:37 PM Ng, vs@ubuntu: -/c/schwifty vs@ubuntu:-/c/schwiftys gcc schwifty.c vs@ubuntu:-/c/schwifty$ ./a.out ex.txt R , o

----------------------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity

Add a comment
Know the answer?
Add Answer to:
Programming in C. Name this program schwifty.c - This program reads a text file and makes...
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
  • USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

    USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...

  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

  • I need help programming this program in C. 1.) Write a program that reads a message,...

    I need help programming this program in C. 1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below: Enter a message: He lived as a devil, eh? Palindrome Enter a message: Madam, I am Adam. Not a Palindrome 2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the...

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • C++ Write a program that prompts for a file name and then reads the file to...

    C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on.

  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

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