Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Reverse words on line Objectives (c program)

Max
Max 


Reverse words on line Objectives • Practice taking program arguments • Practice using C libraries and functions • Practice converting strings to integers • Practice manipulating C strings • Practice top-down program design and reading pseudocode Overview For this lab, you will write a program that reads N lines of data from the command- line, one entire line at a time, and reverses the words of each line. In this example, N = 1: [esus] $ ./lab8 1 the quick brown fox jumps over the lazy dog dog lazy the over jumps fox brown quick the N is a number provided via command-line argument. Details Requirements: • Take in a single command-line argument. o Print an error message if not enough or too many program arguments o Convert it to an integer (print error and quit if entered less than 0) • Read in that many lines from standard input. o For each line, scan the entire line using fgets. • Implement a function to reverse the words in the line, and call it on every input line. o The words must appear in reverse order, but still readable from left to right. o NOTE: Punctuation and extra spaces should be omitted in the reversed string • Print out the reversed line Command-line argument Command-line arguments are a way for you to provide input to the program at the moment it is executed. For this program, you will use a single command-line argument to provide the number of lines to be read from standard input, as an integer greater than or equal to zero. Convert string to int To obtain the value of N from this command-line argument, you must convert it to an int. Use the function strtol to accomplish this. With the call to strtol, you must provide the 'base' of the number returned. In our case, we want a standard 'base 10' number returned. In addition, you will need to use type casting to convert the result of the function back into an int. int N = (int) strtol( str, NULL, 10); NOTE: Be sure to #include to use this function. Standard Input using fgets In this lab, you will use a function called fgets to read input from the keyboard. This function is also used to read data from files, however we can use it to read keyboard input by specifying the global variable stdin as the file pointer parameter. Up until now, we have typically used scanf to accomplish this, but fgets is actually better, because we can control how much data is read in. This helps to prevent buffer overflow errors, in which the string is stored in memory that is not allocated for it. To read input in this way, use the function like this: #define SIZE 80 ... char str[SIZE]; if ( fgets( str, SIZE, stdin ) ) { // input was successful } Algorithm to reverse a string You may attempt to design an algorithm to solve this problem on your own if you'd like. However, you may also implement a C language version of the pseudocode provided below. PROCEDURE reverse( string line ): declare local strings: temp, word idx <- string length of line word_len <- 0 // scan thru characters one by one loop until idx < 0: if line[ idx ] is a space and word_len > 0: copy word_len characters from line to word concatenate word + " " on end of temp word_len <- 0 // reset word length to 0 else if line[ idx ] is alphanumeric: word_len <- word_len + 1 end if end loop // copy over the last word after the loop (if any) if word_len > 0: copy word_len characters from line to word concatenate word on end of temp end if line <- temp // copy temp back into line end PROCEDURE Functions of interest stdio.h: • fgets o Read data from files or standard input stdlib.h: • strtol o Convert a string to a long integer ctype.h: • isalnum o Checks if a character is alphanumeric string.h: • memset o Sets memory to a specific value. Useful to re-initialize local string variables to 0 for re-use in a loop. • strncpy o Copies some number of characters out of a string. • strlen o Gets the number of characters in a string (not including the null terminator). • strcat o Concatenates one string onto another. • strchr o Searches a string for the first occurrance of the given character Example Execution [esus] $ ./lab8 ERROR: Please provide an integer greater than or equal to 0 [esus] $ ./lab8 -3 ERROR: Please provide an integer greater than or equal to 0 [esus] $ ./lab8 2 the quick brown fox jumps over the lazy dog dog lazy the over jumps fox brown quick the what is love love is what [esus] $ ./lab8 1 a test... i demand a test! test a demand i test a Compile & Test Compile your program using this gcc command. c99 is a shortcut for running gcc - std=c99, which uses the C99 standard instead of the default C89 standard. $ c99 -Wall lab8.c -o lab8 NOTE: Make sure you output what is expected, nothing more and nothing less. Resources • You should attend the Friday Lab session to seek assistance from the TAs and CAs. • For specific questions, attend the weekly lab session or attend the TA's office hours. • You are encouraged to use resources or tutorials on the internet to learn unix or C. Check the class resource list for some links to resources. Attachments Tags Clarifications

Answers

C program to reverse the line

vkbhardwaj
vkbhardwaj 



Program works exactly as it is described in the document it reverse the whole line. Attachments Screenshots
Purchase Answer

Reverse words on line

felo
felo 



Find attached Solution Attachments Screenshots
Purchase Answer