EPSII 59:006 Spring 2004.

Slides:



Advertisements
Similar presentations
Strings.
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Character Arrays strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */ char pmessage[] = "now.
Searching Arrays Linear search Binary search small arrays
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Cryptography.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Searching Arrays Linear search Binary search small arrays
LINKED LISTS.
The Machine Model Memory
Strings CSCI 112: Programming in C.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Fundamentals of Characters and Strings
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages and Paradigms
Command Line Arguments
Command line arguments
Command Line Arguments
Understand argc and argv
CSE 1320 Search, Sort and Strings
Lecture-5 Arrays.
Command Line Arguments
CSE 303 Concepts and Tools for Software Development
CSE 351 Section 1: HW 0 + Intro to C
Programming Paradigms
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
Computer Science 210 Computer Organization
Arrays in C.
Character Strings Lesson Outline
More Examples of argc and argv
Command-line Arguments
Command Line Arguments
Computer Science 210 Computer Organization
Command Line Arguments
C Stuff CS 2308.
And now for something completely different . . .
Searching and Sorting Arrays
Command Line Parameters
Logical Operations In Matlab.
24 Searching and Sorting.
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
C++ Pointers and Strings
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
Homework Continue with K&R Chapter 5 Skipping sections for now
EECE.2160 ECE Application Programming
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Character Arrays char string1[] = “first”;
Characters and Strings Functions
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C++ Pointers and Strings
15213 C Primer 17 September 2002.
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

EPSII 59:006 Spring 2004

A Design Example Illustrates: Pointers and arrays of pointers command line arguments simple character string processing

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 %

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

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

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.

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

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

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

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

Comparing Strings—The Cases: A ==B Case: ‘x’ A … ‘y’ ‘\0’ B ‘x’ … ‘y’ ‘\0’ strings match in this portion

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

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.

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()

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;

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()

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

Have a Great Spring Break!!!