Unformatted text preview:

StringsCharacter StringsSlide 3Slide 4String I/O Library RoutinesSlide 6Slide 7String Handling Library RoutinesSlide 9Arrays, Strings, and PointersArrays, Strings, and PointersSorting Strings with PointersSlide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 1Winter QuarterStringsLecture 16Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 2Winter QuarterCharacter 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 ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 3Winter QuarterCharacter 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 ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 4Winter QuarterCharacter Strings•Declaration and initialization:char color [ ] = "scarlet" ;orchar *colorPtr = "scarlet" ;orchar color [8] = {'s', 'c', 'a', 'r', 'l', 'e', 't', '\0'} ;•NOTE: Allowance must always be made for the terminating null character.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 5Winter QuarterString 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) ; // Do not use, everEngineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 6Winter QuarterString 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, … ) ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 7Winter QuarterString 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 */Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 8Winter QuarterString 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" */Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 9Winter QuarterString 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. */Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 10Winter QuarterArrays, 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 ;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 11Winter QuarterArrays, Strings, and Pointersvoid 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 */18Richard J. Freuler blanksEngineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 12Winter QuarterSorting 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 ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 16 P. 13Winter QuarterSorting 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


View Full Document

OSU ENGR H192 - Lecture 16 - Strings

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 16 - Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 16 - Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 16 - Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?