Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education...

23
Lect 16 P. 1 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition Strings Lecture 16

Transcript of Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education...

Page 1: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 1

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Strings

Lecture 16

Page 2: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 2

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Character Strings

• Up until now, we have dealt mostly with simple character variables. A variable of type char may hold a single character and is assigned a value using a pair of single quotes:– Example: char onechar = 'z' ;

• On the other hand, character strings are arrays of simple characters with a special character inserted into the string at the very end. They are assigned values with a pair of double quotes:– Example: char arraychar[6] = "abcde" ;

Page 3: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 3

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Character Strings

• This array of characters, or string, ends in the special null character ( '\0' ).

• Strings are normally accessed by a pointer to the first character in the string. This means that the value of a string is the address of it's first character. Thus we say that a string is a pointer, mostly because we often use the string name in manipulating the string. Since the string name is the name of the array of characters, it is a pointer like the name of any other array in C is a pointer.

Page 4: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 4

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Character Strings

• Declaration and initialization:

char color [ ] = "scarlet" ;or

char *colorPtr = "scarlet" ;or

char color [8] = {'s', 'c', 'a', 'r', 'l', 'e', 't', '\0'} ;

• NOTE: Allowance must always be made for the terminating null character.

Page 5: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 5

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

String I/O Library Routines• #include <stdio.h>

/* The following are function prototypes for some of the String I/O and Handling Library Routines */

/* Input next character as an integer */

int getchar (void) ;

/* Input string into array s until newline */

char *gets (char *s) ;

Page 6: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 6

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

String I/O Library Routines• #include <stdio.h>

/* Print character stored in character variable c */

int putchar (int c) ;

/* Print character string s followed by \n */

int puts (const char *s) ;

/* Performs scanf function on string s */

int sscanf (char *s, const char *format, … ) ;

Page 7: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 7

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

String I/O Library Routines• #include <stdio.h> <notice!!! – from stdio lib !!!

/* To input a number of characters from a stream */

char *fgets (char *string1, int n, FILE *stream) ;

/* where up to (n - 1) characters are accepted, including newline */

/* To output a string of characters to a stream */

char *fputs (char *string1, FILE *stream) ;

/* where all the characters are placed into the I/O stream */

Page 8: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 8

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

String Handling Library Routines• #include <string.h> < Notice !!! – from string lib!!!

/* To copy string 2 into string 1 */

char *strcpy (char *string1, const char *string2) ;

/* To append string 2 to the end of string 1 */

char *strcat (char *string1, const char *string2) ;

/* where first character of string 2 replaces the null in string 1 */

/* To find out length of string 1 (# of characters) */

size_t strlen (const char *string1) ;

/* where "size_t" is either an "unsigned int" or "unsigned long" */

Page 9: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 9

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

String Handling Library Routines

• #include <string.h>

/* Compare string 1 to string 2. The function returns 0, less than 0, or greater than 0 if string 1 is equal to, less than, or greater than string 2 respectively. */

int strcmp (const char *string1, const char *string2 ) ;

/* In this context, less than and greater than depend on the integer values assigned to each of the the individual characters. In the ASCII scheme of things, the integer values (or character codes) are assigned in an order so that we can do things alphabetically, i.e., 'a' is less than 'b' is less than 'c', and so on. */

Page 10: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 10

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Arrays, Strings, and Pointers

/* Passing an array to a function */

#include <stdio.h>

#include <string.h>

void mysub ( char [ ] ) ;

int main ( )

{

char name[20] = "Richard J. Freuler" ;

mysub (name) ;

return 0 ;

}

Page 11: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 11

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Arrays, Strings, and Pointers

void mysub ( char text[ ] ){ int len, k ; len = strlen (text) ; printf ("%d\n", len ) ; for (k = 0 ; k < len ; k++) printf ("%c", text [k] ) ;}/*Program Output */18RichardJ.Freuler� �

blanks

Page 12: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 12

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

• A character string is actually just a one-dimensional array of simple character variables.

• So, an array of character strings is really an array of arrays, that is, a two-dimensional array.

• It might be declared as follows:char mystrings [ m ] [ n ] ;

where m is the number of rows (strings) in the array and n is the maximum number of characters in each string.

Page 13: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 13

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers• Now, C has to know how many characters are

actually stored in each string. Recall that a string termination character, or null, ('\0') is inserted after the last character.

• Note that a string termination character takes one array element to store, therefore the maximum string length that could be stored in one row of the array mystrings is n - 1.

• Often, a string is stored with a newline character ('\n'), which also takes a character space. Thus, the max string length can be reduced to n - 2.

Page 14: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 14

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers• The address of the beginning of the array

mystrings is just the array name mystrings without any subscripts.

• The address of any string (any row) in the array is mystrings[k] where k is the row number.

• Since all subscripts start at zero in C, mystrings and mystrings[0] are effectively the same address.

Page 15: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 15

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

• Strings can be sorted using an array of pointers. Since the first element of each string (each row) is the address of the string, that address can be assigned to one of the pointers.

• When address of each string (or row) is assigned to a pointer, the array of pointers can be sorted and used to retrieve the strings in the new order.

• The order (location) of the actual strings in memory will NOT be changed.

Page 16: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 16

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

/* This program sorts strings using array of pointers */

#include <stdio.h>

#include <string.h>

#define SIZE 4

int main ( )

{

int k, swaps ;

char name[SIZE][30], *nptr[SIZE], *temp ;

char filename[ ] = "namelist.dat" ;

FILE *fptr ;

fptr = fopen (filename,"r") ;

Page 17: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 17

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

printf ("\n Original list\n") ;

for (k = 0 ; k < SIZE ; k++) /* Assign addresses */

{ /* to pointers in array */

nptr[k] = name[k] ;

fgets (name[k], 30, fptr) ;

printf ("%s", name[k]) ; /* Print original list */

}

Page 18: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 18

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

M I R A N D A \n \0J A S O N \n \0A N D Y \n \0M E G A N \n \0

0 1 2 3 4 5 … 290123

So, the name array might look like:

And, assuming name array started at memory location xxx200, the nptr array would look like:

0 1 2 3200 230 260 290

Page 19: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 19

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

do /* Sort the pointers in ascending alphabetic */{ /* order of the strings */ swaps = 0 ; for (k = 0 ; k < (SIZE - 1) ; k++) { if (strcmp (nptr[k], nptr[k+1]) > 0) { temp = nptr[k] ; nptr[k] = nptr[k+1] ; nptr[k+1] = temp ; swaps=1 ; } }} while (swaps);

Page 20: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 20

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

0 1 2 3 4 5 … 290123

So, the name array would still look like:

And, the nptr array would end up as:

260 230 290 200 0 1 2 3

M I R A N D A \n \0J A S O N \n \0A N D Y \n \0M E G A N \n \0

Page 21: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 21

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers

printf ("\n New list\n") ;

for (k = 0 ; k < SIZE ; k++) printf ("%s", nptr[k]) ;

printf ("\n Original list again\n") ;

for (k = 0 ; k < SIZE ; k++) printf ("%s", name[k]) ;

printf ("\n") ;

}

Page 22: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 22

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Sorting Strings with Pointers Original listSmith, Robert A.Sanderson, John T.Alberts, Mary C.Dreese, Edward A. New listAlberts, Mary C.Dreese, Edward A.Sanderson, John T.Smith, Robert A.

Original list againSmith, Robert A.Sanderson, John T.Alberts, Mary C.Dreese, Edward A.

Page 23: Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.

Lect 16 P. 23

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Assignment G14

• The previous example provides the essential elements for solving G14.

• Suggestions:– Copy file to your directory– Use ‘more’ to see what is in the file– First write your program to read and print the

contents of the file– Then develop your sorting routine for the file.