Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Print an error message if not enough or too many program arguments

Convert it to an integer (print error and quit if entered less than 0)

Read in that many lines from standard input.

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.

The words must appear in reverse order, but still readable from left to right.

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

Read data from files or standard input

stdlib.h:

strtol

Convert a string to a long integer

ctype.h:

isalnum

Checks if a character is alphanumeric

string.h:

memset

Sets memory to a specific value. Useful to re-initialize local string variables to 0 for re-use in a loop.

strncpy

Copies some number of characters out of a string.

strlen

Gets the number of characters in a string (not including the null terminator).

strcat

Concatenates one string onto another.

strchr

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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago