CS240 Spring 2011: Lab03
Arrays and String Manipulation
Topics Covered:
Command line arguments, loops, conditional statements, one dimensional
arrays, string manipulation (copy, concatenate, compare, reverse),
functions, pointers, recursion, passing arrays to functions,
redirection.
Task 0: Downloading the Initial Source Files
Download the file lab3-src.tar and untar it
by typing the command:
tar -xvf lab3-src.tar
The directory lab3-src contains the initial source files as well as the
script testall that will test all your programs. To build your programs
type "make" inside the directory lab3-src ("Makefile" is provided to
you). Make sure you compile and
test your programs in lore.cs.purdue.edu.
Note: If you get a permission denied error, type the following in your
lab3-src folder:
chmod 777 *.*
chmod 777 testall
Task 1: Reverse a String using Recursion
Write a program to reverse a string, entered as a command line
argument, recursively. The format of executing your program should be:
reverse <string>
E.g. reverse Purdue University
The output should be:
Reverse of String = ytisrevinU eudruP
Note that the above example contains three arguments. You can verify
the count using the value of "argc". These three arguments are as
follows:
argv[0] =
"reverse"
argv[1] =
"Purdue"
argv[2] =
"University"
Since you have to reverse the complete string "Purdue University", you
must first concatenate the arguments together (except "reverse"). You
can use the inbuilt string function strcat() for concatenating two
strings.
Using strcat():
The strcat() function appends the source string to the
destination string, overwriting the null byte ('\0') at the end of
destination string, and then adds a terminating null byte.
The destination string must have enough space for the result.
"string.h" header file must be included before using the string
functions.
E.g.
#include
<stdio.h>
#include
<string.h>
int main(int
argc, char ** argv){
char destination[50], source[50];
strcpy(source, "Lab"); // copies the String "Lab" in
"source" character array
strcpy(destination, "CS240"); // copies the String
"CS240" in "destination" character array
strcat(destination, " "); // appends a space " "
after the string stored in "destination" (which is CS240) and stores
the result back to
"destination"
strcat(destination, source); // appends the string
stored in "source" (which is Lab) after the string stored in
"destination" and stores
the result back to "destination"
printf("%s\n", destination);
}
The output of the above program is : CS240 Lab
Use man pages for finding more information about the usage of different
functions. E.g. man strcat
Now you can make a complete string using the arguments entered at the
command line. You have to reverse this string using recursion. The
method of solving a problem using recursion involves a function that
calls itself again and again till the base case is satisfied. E.g.
Factorial of a number can be found out using recursion - fact(5) = 5 *
fact(4) = 5*4*fact(3)... and so on.
A file named "reverse.c"
has
been
provided
to
you which contains the skeleton of the program. The
reverse () function, specified in the file, takes a string as
an argument. This function must be called recursively to display the
reverse
of the string. You must also
perform an input validation check which should ensure that the correct
number of arguments are passed to the program; if not then it must
display an appropriate message on the screen and exit the program:
Usage: reverse
<string>
There is a file named "reverse.org" in your lab3-src folder which is an
implementation of this program. Please try to execute this reference
program to understand the expected behavior. A "testall" script is
given to you to let you know if your program's implementation is
correct. To run testall type:
testall
Few examples for using "reverse.org" are:
Ex.1
Input:
reverse.org
Output:
Usage :
reverse <string>
Ex 2.
Input:
reverse.org
CS240
Output:
Reverse of
String = 042SC
Task 2: Substring Search
Write a program to display all lines in a given file which contains a
given string. The format of executing your program should be:
mygrep {String to Search} <{File Name}
E.g. mygrep CS240 <input.txt
Here, "CS240" is the string to be searched in the file named
"input.txt". The program must display all the lines which contain the
word "CS240". If the contents of the "input.txt" file are:
This is CS240
Lab.
CS240 Spring
2011.
Purdue
University, West Lafayette, Indiana
Indiana
Then the output should be:
This is CS240
Lab.
CS240 Spring
2011.
The symbol "<" is used for redirecting the standard input stream to
a file. So, instead of reading the input from the command prompt, the
program will take input from the file specified after "<". Also,
notice that the mygrep command is case sensitive (Purdue and purdue are
different). The program is named "mygrep" as it imitates the behavior
of the "grep" command line text search utility for UNIX.
Use function getchar to read the input file (one character at a time). Recall that each line
in a text file is terminated by the newline ('\n') character.
A file named "mygrep.c"
has been provided to you which contains the skeleton of the program.
You have to implement the isPresent () function which takes the
"mainString" and "subString" as arguments and returns a 1 if the
"mainString" contains the "subString", else it returns 0. You must also
perform an input validation check which should ensure that the correct
number of arguments are passed to the program; if not then it must
display an appropriate message and exit the program:
Usage : mygrep
{String to Search} <{File Name}
There is a file named "mygrep.org" in your lab3-src folder which is an
implementation of this program. Please try to execute this reference
program to understand the expected behavior. A "testall" script is
given to you to let you know if your program's implementation is
correct. To run testall type:
testall
Few examples for using "mygrep.org" are:
Ex.1
Input:
mygrep.org
Output:
Usage : mygrep
{String to Search} <{File Name}
Ex 2.
Input:
mygrep.org
Purdue <input.txt
Output:
Purdue
University, West Lafayette.
The script testall
A script testall has been
provided to test your project. Make sure you run this script since it
will be used to grade your project.
Note: We will change the test cases to ensure that your programs do
work for all possible test cases and not just the test cases that have
been mentioned in the testall provided to you.
Submit
Follow these instructions to turnin lab3:
- Login to lore.cs.purdue.edu
- Run "make clean" in your lab3-src folder.
- Change to the parent directory above lab3-src.
- Type "turnin -v -c cs240=XXX -p lab03 lab3-src". Replace XXX with your section number:
9:30 am - 11:20 am F | F930 |
11:30 am - 1:20 pm F | F1130 |
1:30 pm - 3:20 pm F | F130 |
3:30 pm - 5:20 pm F | F330 |
9:30 am - 11:20 am R | R930 |
11:30 am - 1:20 pm R | R1130 |
3:30 pm - 5:20 pm R | R330 |
11:30 am - 1:20 pm T | T1130 |
- Type "turnin -c cs240=XXX -p lab03 -v" to make sure you have
submitted the right files.
The deadline for this lab is Wednesday,
February 9th, 11:59 PM