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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Introduction to C Programming
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and Final Review Topics Lecture.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lectures 10 & 11.
1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm 2 Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 15P. 1Winter Quarter Arrays and Pointers.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Chapter 15 Strings as Character Arrays
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm II Review Topics Lecture M2.
Characters and Strings
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
char first[10]="monkey"; char second[10]="fish"; char* keep;
User-Written Functions
Characters and Strings
Basic notes on pointers in C
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Characters and Strings
C Characters and Strings
Presentation transcript:

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

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 2Winter Quarter 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" ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 3Winter Quarter 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.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 4Winter Quarter 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.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 5Winter Quarter String I/O Library Routines #include /* 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) ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 6Winter Quarter String I/O Library Routines #include /* 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, … ) ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 7Winter Quarter String I/O Library Routines #include <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 */

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 8Winter Quarter String Handling Library Routines #include < 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" */

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 9Winter Quarter String Handling Library Routines #include /* 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. */

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 10Winter Quarter Arrays, Strings, and Pointers /* Passing an array to a function */ #include void mysub ( char [ ] ) ; int main ( ) { char name[20] = "Richard J. Freuler" ; mysub (name) ; return 0 ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 11Winter Quarter 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 */ 18 RichardJ.Freuler blanks

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 12Winter Quarter 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.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 13Winter Quarter 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.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 14Winter Quarter 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.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 15Winter Quarter 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.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 16Winter Quarter Sorting Strings with Pointers /* This program sorts strings using array of pointers */ #include #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") ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 17Winter Quarter 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 */ }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 18Winter Quarter Sorting Strings with Pointers … So, the name array might look like: And, assuming name array started at memory location xxx200, the nptr array would look like:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 19Winter Quarter 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);

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 20Winter Quarter Sorting Strings with Pointers … So, the name array would still look like: And, the nptr array would end up as:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 21Winter Quarter 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") ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 22Winter Quarter Sorting Strings with Pointers Original list Smith, Robert A. Sanderson, John T. Alberts, Mary C. Dreese, Edward A. New list Alberts, Mary C. Dreese, Edward A. Sanderson, John T. Smith, Robert A. Original list again Smith, Robert A. Sanderson, John T. Alberts, Mary C. Dreese, Edward A.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 23Winter Quarter 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.