© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Strings.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 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.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 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.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Topic 10 - Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
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.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
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.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students C-strings and strings.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
13. 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.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
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)
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Strings CSCI 112: Programming in C.
C Characters and Strings
© 2016 Pearson Education, Ltd. All rights reserved.
A First Book of ANSI C Fourth Edition
Arrays in C.
C Characters and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Strings

© Janice Regan, CMPT 102, Sept Simple and Composite Variables  We have studied simple variables  A simple variable describes a single value  A simple variable has an identifier  A simple variable has a type that describes the properties of the value of the variable, the permissible operations for the variable, and the representation of the variable in computer memory  We can also have composite variables  These variables describe a group of values  Arrays: all values in the group have the same type  Structures: different values in the group can have different types

© Janice Regan, CMPT 102, Sept Composite Variables  composite variables describe a group of values  1 dimensional arrays or variables of a particular type (all entries must have the same type)  multi dimensional arrays or variables of a particular type (all entries must have the same type)  Structures containing groups of variables of different types  Strings are another special type that builds on arrays  An array of characters  A set of special operations appropriate for text

© Janice Regan, CMPT 102, Sept One-Dimensional (1-D) Arrays  An array is an indexed data structure  All variables stored in an array are of the same data type  An element of an array is accessed using the array name and an index or subscript  The name of the array is the address of the first element and the subscript is the offset  In C, the subscripts always start with 0 and increment by 1

© Janice Regan, CMPT 102, Sept Example String Declaration in C char list[10];  allocates memory for 10 characters.  Ten adjacent locations in memory are allocated  Remember C does not perform any bounds checking on arrays list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[9] list[8]

© Janice Regan, CMPT 102, Sept Initializing 1-D Arrays  Strings are not the same as 1-D character arrays  You can specify individual values for each character in a 1-D character array /* put one character in each element of the array*/ char list[8] = {‘h’,’e’,’l’,’l’, ‘o’}; After initialization memory looks like list[0] ‘h’ list[1] ‘e’ list[2] ‘l’ list[3] ‘l’ list[4] ‘o’ list[5] ? list[6] ? list[7] ? list[8] ?

© Janice Regan, CMPT 102, Sept Difference: String vs 1D character array  Method of initialization  A string always in a null termination character (\0)  This tells all the functions in the string library where the string ends  Use of the null termination character allows strings of different length to be stored in a character array of a single length

© Janice Regan, CMPT 102, Sept Strings of different lengths  Strings of different lengths can be stored in a character array  The maximum number of character in the string is the number of characters in the array minus one  Blanks can be included in the string  Blanks count as characters char list[8] = {“hello”}; char list1[8] = {“hi jane”}; list[0] ‘h’ list[1] ‘i’ list[2] ‘ ’ list[3] ‘j’ list[4] ‘a’ list[5] ‘n’ list[6] ‘e’ list[7] ‘\0’ list[8] ? list[0] ‘h’ list[1] ‘e’ list[2] ‘l’ list[3] ‘l’ list[4] ‘o’ list[5] ‘\0’ list[6] ? list[7] ? list[8] ?

© Janice Regan, CMPT 102, Sept Avoid a common problem (1)  C does not perform any bounds checking on arrays  This means that you can accidentally change the values of other variables by changing a value you refer to as an element of the array, which is not actually part of the array  For a string variable this is particularly easy. You must remember that character array mystring[20] holds a string of no more than 19 characters “hello my friend” has 15+1 characters “joe” has 3+1 characters REMEMBER THE NULL TERMINATION CHARACTER

© Janice Regan, CMPT 102, Sept Avoid a common problem (2) int count = 3; char myArray[5] = {“hello”}; After the first declaration memory looks like After the second declaration statement above REMEMBER: Leave room for the \0 myArray[0] ‘h’ myArray[1] ‘e’ myArray[2] ‘l’ myArray[3] ‘l’ myArray[4] ‘o’ count ‘\0” myArray[0] ? myArray[1] ? myArray[2] ? myArray[3] ? myArray[4] ? count 3

© Janice Regan, CMPT 102, Sept Avoid a common problem (3) int count [4]= {1,2,3,5}; char mySt[5] = {“my name”}; After the first declaration memory looks like After the second declaration statement above mySt has no terminating \0, string library breaks Array count has been corrupted and now contains the integer equivalent of “e” mySt[0] ? mySt[1] ? mySt[2] ? mySt[3] ? mySt[4] ? Count[0] 1 Count[1] 2 Count[2] 3 Count[3] 5 mySt[0] ‘m’ mySt[1] ‘y’ mySt[2] ‘ mySt[3] ‘n’ mySt[4] ‘a’ Count[0] ‘m’ Count[1] ‘\0’ Count[2] 3 Count[3] 5

© Janice Regan, CMPT 102, Sept Avoid a common problem (4)  C does not perform any bounds checking on arrays  By initializing or changing the contents of a string with a string that is longer than will fit into the character array associated with the string it is possible to change the value of a completely different variable and to break the string library for the string being initialized  It is imperative that you be very careful to avoid using strings longer than the allocated space

© Janice Regan, CMPT 102, Sept Arrays of strings  Declare your array of Strings #define NUMNAMES 20 #define MAXNAMELEN 32 char names[NUMNAMES][MAXNAMELEN]  Declare and Initialize your array #define NUMMONTHS 12 #define MONTHNAMESIZE 10 char month[NUMMONTHS][MONTHNAMESIZE] = { “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, December” };

© Janice Regan, CMPT 102, Sept Initializing and array of strings Char month[12][10] = { “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, December” }; ‘J’ ‘a’‘n’‘u’‘a’‘r’ ‘y’ ‘\0’?? ‘F’ ‘e’‘b’‘r’‘u’‘a’ ‘r’ ‘y’‘\0’? ‘M’ ‘a’‘r’‘c’‘h’‘\0’ ? ??? ‘A’ ‘p’‘r’‘i’‘l’‘\0’ ? ??? ‘M’ ‘a’‘y’‘\0’??? ? ??? ‘J’ ‘u’‘n’‘e’‘\0’? ? ??? ‘J’ ‘u’‘l’‘y’‘\0’? ? ??? ‘A’ ‘u’‘g’‘u’‘s’‘t’ ‘0’ ??? ‘\0’

© Janice Regan, CMPT 102, Sept Initializing a string  You can initialize a simple variable either in the declaration statement, or using separate assignment statements following the declaration statements.  You can also initialize the values of the string following using assignment statements following the declaration statements  When initializing a string remember to be sure that you do not put more characters in the string than there is space for

© Janice Regan, CMPT 102, Sept Initializing or using 1-D arrays char *list[10]; int i; /*Initialize each element of array list to “mystart” */ for(i=0; i<10; i++) { strcpy(list[i], “mystart”); }

© Janice Regan, CMPT 102, Sept Putting data into a 1-D Array  Another common way of assigning values to strings or arrays of strings is to read data values from a file directly into the string or array of strings  Each value read from the file is assigned to a single string(for example names[6])  A single row stored in the ith row in the array of strings names is referred to as names[i],  Note that checks to determine the file was opened correctly and that data was read correctly have been omitted from the example, they should not be omitted from your code

© Janice Regan, CMPT 102, Sept Array Input from a data file #define NUMPEOPLE 30 #define NAMELEN 32 char names[NUMPEOPLE][NAMELEN]; char title[30]; int ages[NUMPEOPLE]; int k; FILE *registrants; registrants = fopen(“registrants”, “r”); scanf( “%s”, title); printf(“%s\n”, title); for(k=0; k<NUMPEOPLE; k++) { fscanf(registrants, “%s %d”, names[k], &ages[k]); printf(“%33s, %3d\n”, names[k], ages[k]); }

© Janice Regan, CMPT 102, Sept Notes on array input  The string is read or written using %s  When you read or write a string your read or write all characters in that string  The final character in the string is determined by the location of the null termination character \0  When reading a string using scanf or fscanf there is no & before the name of the string  The name of the string is a reference (address) of the first element of the string

© Janice Regan, CMPT 102, Sept Strings as function parameters  Arrays, or parts of arrays, can be passed as arguments to functions.  An element of an string can be used as a simple character variable parameter It can be passed by value or by reference  An entire string can be used as a parameter of a function It can only be passed by reference using the name of the string (the name of the string is a reference to the location in memory of the first character in the string)

© Janice Regan, CMPT 102, Sept Strings as a data type  Remember a data type has a group of objects (things) that can be combined in different ways using the operands for that data type.  The operands for strings are not those used for other data types (like +, -, = …)  All operations on strings are performed using functions from the string library (other than reading and writing)  To include the string library in your program include

© Janice Regan, CMPT 102, Sept Assigning strings  You have seen that = can be used when assigning values to strings in a declaration  = cannot be used to assign a string literal to a string: The following is not valid mystring = “testinput”;  To copy one string to another the string library functions strcpy is usually used strcpy(mystring, “testinput”); strcpy(myCopiedString, myOriginalString);

© Janice Regan, CMPT 102, Sept Assigning strings  To copy one string to another the string library functions strcpy is usually used  To copy part of a string, (a substring) or to assure you do not copy more characters into a string that it can hold you can also use strncpy strncpy(mystring, “testinput”, 5);  Note that strncpy copies the first 5 characters of “testinput” only (testi) and does not add a \0 to the end of the copied string  strcpy copies the entire string (even it it is longer than the available space!) including the \0

© Janice Regan, CMPT 102, Sept Avoid a common problem (3) int count [4]= {1,2,3,5}; char mySt[5] ; strcpy(mySt, “my name”); After the declarations memory looks like After the strcpy statement above mySt has no terminating \0 in its array mySt[0] ? mySt[1] ? mySt[2] ? mySt[3] ? mySt[4] ? Count[0] 1 Count[1] 2 Count[2] 3 Count[3] 5 mySt[0] ‘m’ mySt[1] ‘y’ mySt[2] ‘ mySt[3] ‘n’ mySt[4] ‘a’ Count[0] ‘m’ Count[1] ‘e’ Count[2] ‘\0’ Count[3] 5

© Janice Regan, CMPT 102, Sept Finding the length of a string  To find the number of characters actually stored in a string use the string library function strlen int len; char mystring[30]; strcpy(mystring, “testing”); len = strlen(mystring); /* len now has a value 7 */  Strlen counts the number of characters in the string not including the terminating \0

© Janice Regan, CMPT 102, Sept Concatenating Strings  Combining two strings into a single string  Use the string library functions strcat or strncat  strcat and strncat take one string and append it to the end of another string  The terminating \0 is removed from the end of the first string before the second string is added  The terminating \0 is replaced at the end of the second string  strcat and strncat can create a string too long to fit in the allocated string storage:

© Janice Regan, CMPT 102, Sept Example: using strcat char name1[10] = “marie”; char name2[10] = “anne”; strcat( name2, name1); marei\0???? ann e????? marei ???? annmeari e

© Janice Regan, CMPT 102, Sept Concatenating Strings char mystring[20]=“start input: “; char mystring1[20] = “input1”; char mystring2[20] = “ and output” /* after the following strcat mystring1 contains */ /* “input1 and output” */ strcat(mystring1, mystring2); /* after the following strcat mystring1 contains */ /* “start input: input1 and output” */ /* this string overflows the array mystring */ strcat(mystring, mystring1);

© Janice Regan, CMPT 102, Sept Example: using strcat char name1[10] = “marie”; char name2[10] = “anne”; strncat( name2, name1, 2); marei\0???? ann e????? marei ???? annmea ???

© Janice Regan, CMPT 102, Sept Concatenating Strings #define STRLEN 20 int len, added; char mystring[STRLEN]=“start input: “; char mystring1[STRLEN] = “input1 and output”; /* To prevent overflow find the number of */ /* characters that can be added to mystring */ /* added (6) = STRLEN (20) – len(13) – 1 */ len = strlen(mystring); added = STRLEN – len -1; /* after the following strcat mystring1 contains */ /* “start input: input1” */ strncat(mystring, mystring1, added);

© Janice Regan, CMPT 102, Sept Comparing Strings  To compare 2 strings usually use the string library function strcmp strcmp(mystring1, mystring2)  strcmp returns an integer, if mystring1 is alphabetically before mystring2 a negative number will be returned If the strings are identical 0 will be returned if mystring2 is alphabetically before mystring1 a positive number will be returned

© Janice Regan, CMPT 102, Sept ASCII equivalents  Each alphabetic character, number, or other character (including whitespace characters) has an integer equivalent value  These integer values are used by strcmp to determine the alphabetical ordering.  All uppercase letters precede lower case letters  All numbers precede uppercase letter  A string st1 contains the first few characters of a longer string st2. st1 precedes st2 when compared

© Janice Regan, CMPT 102, Sept Comparing Parts of Strings  To compare the first n characters of 2 strings use the string library function strncmp strncmp(mystring1, mystring2, n)  strncmp returns an integer, if mystring1 is alphabetically before mystring2 a negative number will be returned If the strings are identical 0 will be returned if mystring2 is alphabetically before mystring1 a positive number will be returned

© Janice Regan, CMPT 102, Sept Conversion to and from strings  Strings to numbers: sscanf  Works like fscanf bur read from a string  Numbers to strings: sprintf  Works just like fprintf but writes into a string char mystring[20]; int myvalue1= 23, myvalue2=46; sprintf( mystring, “%s:%2d, %2d”, “myvalues are”, myvalue1, myvalue2); /* now mystring contains */ /* myvalues are:23, 46 */

© Janice Regan, CMPT 102, Sept Character analysis  You can also analyze a string (or a character array) one character at a time  The ctype library #include includes functions for such analysis. Each of these functions returns an integer value. The value is nonzero if the condition checked is true (0 if it is false) isalpha(char mychar); /* is an alphanumeric char */ isdigit( char mychar); /* is a numeral */ ispunct(char mychar); /* is a non whitespace punctuation character */ isspace(char mychar); /* is a whitespace character */ tolower(char mychar); /* converts alphanumeric to lower case */ toupper(char mychar); /* converts alphanumeric to upper case */h

© Janice Regan, CMPT 102, Sept The ctype and string Libraries  We have had an introduction to some of the functions in these libraries.  These libraries are much more flexible than this subset of functions indicates  You should be able to read the function descriptions for the other functions in the string library and then use those functions in your programs