Download presentation
Presentation is loading. Please wait.
1
EPSII 59:006 Spring 2004
2
A Design Example Illustrates: Pointers and arrays of pointers
command line arguments simple character string processing
3
The Objective: Develop a program that will alphabetize a list of
words typed as command line arguments Example: %gcc alphabetize.c –o alphabetize %alphabetize how now brown cow brown cow how now %
4
How to approach this problem:
Will need to use: main(int argc, char *argv[]) { to capture the command line arguments Will need to bubble sort the strings in argv[] into alphabetical order Will need to figure out how to compare character strings
5
Dealing with the command line arguments:
argv[0] ‘a’ ‘l’ ‘p’ ‘h’ ‘a’ ‘b’ ‘e’ ‘t’ ‘i’ ‘z’ ‘e’ ‘\0’ argv[1] ‘h’ ‘o’ ‘w’ ‘\0’ argv[2] ‘n’ ‘o’ ‘w’ ‘\0’ argv[3] ‘b’ ‘r’ ‘o’ ‘w’ ‘n’ ‘\0’ argc 5 argv[4] ‘c’ ‘o’ ‘w’ ‘\0’ Need to sort the strings argv[1] through argv[argc-1] into alphabetical order
6
argv[] after sorting argv[0] ‘a’ ‘l’ ‘p’ ‘h’ ‘a’ ‘b’ ‘e’ ‘t’ ‘i’ ‘z’
‘\0’ argv[1] ‘b’ ‘r’ ‘o’ ‘w’ ‘n’ ‘\0’ argv[2] ‘c’ ‘o’ ‘w’ ‘\0’ argv[3] ‘h’ ‘o’ ‘w’ ‘\0’ argv[4] ‘n’ ‘o’ ‘w’ ‘\0’ Note: We only need to sort the pointers.
7
The Algorithm First refinement: Second Refinement:
Sort the command line arguments into alphabetic order and print them Second Refinement: Bubblesort the strings according to increasing lexigraphical (alphabetical) order Print out the ordered strings
8
The Algorithm--Continued
The bubblesort step: This will be fairly straightforward Only twist is that we will just sort the array of pointers (argv[]) We need to come up with a way to compare two character strings to determine which one is “larger”—i.e. comes later in alphabetical order
9
Comparing Character Strings
Need to use the notion of “alphabetical order” of words Can compare individual characters based upon the values of their underlying ASCII representations( ‘a’ < ‘b’ < ‘c < … < ‘z’) See Appendix D in the text for the complete ASCII Character set
10
Comparing Character Strings—The Cases
A < B cases: A > B cases: A ‘x’ … ‘y’ ‘\0’ A ‘x’ … ‘y’ ‘z’ … ‘\0’ B B ‘x’ ‘x’ … ‘y’ … ‘\0’ … ‘y’ ‘\0’ ‘z’ strings match in this portion strings match in this portion A … A ‘x’ … ‘y’ ‘x’ ‘z’ … ‘\0’ … ‘y’ ‘w’ ‘\0’ B ‘x’ … ‘y’ ‘w’ … ‘\0’ B ‘x’ … ‘y’ ‘z’ … ‘\0’ strings match in this portion strings match in this portion
11
Comparing Strings—The Cases:
A ==B Case: ‘x’ A … ‘y’ ‘\0’ B ‘x’ … ‘y’ ‘\0’ strings match in this portion
12
String Comparison Algorithm-Pseudocode
First refinement: Compare two character strings A and B Second Refinement: Starting from the left end find the first position i in which the strings differ or the end of at least one string is reached If string B has reached the end but not string A or if neither string has reached the end but the ith character of A is greater than the ith character of B, then A>B If String A has reached the end but not string B or if neither string has reached the end but the ith character of A is less than the ith character of B, then A < B Otherwise (both strings have reached the end) A ==B
13
The String Comparison Algorithm--Continued
Third refinement: Starting from the left end find the first position i in which the strings differ or the null termination character of at least one string is reached If the ith character of A is greater than the ith character of B, then A>B If the ith character of A is less than the ith character of B, then A < B Otherwise (both strings have reached the end) A ==B Note: This refinement takes advantage of the fact the the null termination charater ‘\0’ is the “smallest character in the ASCII character set.
14
the compareStrings function
int compareStrings(char x[], char y[]) { //returns -1 if string x[] is less than string y[] // returns 0 if string x[] equals string y[] //returns 1 if string x[] > string y[] int i = 0; while ((x[i] == y[i])&&(x[i] != '\0')&&(y[i] != '\0')) i++; if (x[i] > y[i]) return 1; // first string is larger else if (x[i] < y[i]) return -1; // second string is larger else return 0; // strings are equal } //end of compareStrings()
15
The main program for(pass=1; pass<argc; pass++) {
for(j=1; j<argc-1; j++) { if (compareStrings(argv[j], argv[j+1]) ==1) { hold = argv[j]; argv[j] = argv[j+1]; argv[j+1] = hold; } for (j=1; j<argc; j++) { printf("%s\n", argv[j]); } // end of main #include <stdio.h> #include <string.h> #define TRUE 1 #define FALSE 0 int compareStrings(char [], char [] ); int main(int argc, char *argv[]) { int pass, j; char *hold;
16
Another version of compareStrings(), using pointer notation:
int compareStrings(char *x, char *y) { //returns -1 if string *x is less than string *y // returns 0 if string *x equals string *y //returns 1 if string *x > string *y while ((*x == *y)&&(*x != '\0')&&(*y != '\0')) { x++; y++; } if (*x > *y) return 1; // first string is larger else if (*x < *y) return -1; // second string is larger else return 0; // strings are equal } //end of compareStrings()
17
Design Example—Final Notes
Note that our alphabetize program acts funny with respect to upper case letters—e.g. it considers the string “Zzzzz” to come before “aAAAA”. Why???? The C strings library has a built-in function called strcmp that does the same thing as our compareStrings function. The strings library has many powerful string processing functions. This will be the next topic that we cover in this course
18
Have a Great Spring Break!!!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.