Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

 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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
Chapter 8 Arrays and Strings
Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld.
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.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Introduction to C programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
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.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
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.
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.
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).
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays in C.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
CPS120: Introduction to Computer Science
Arrays.
Presentation transcript:

Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld

Strings (arrays of characters) We have seen strings before: –When using printf, the stuff between the quotes is a string printf( “ Hi, I am a string!\n ” ); Notice that strings are represented with double quotes and are really arrays of chars (represented with single quotes) So how can we declare these strings?

Declaring strings char cMyFirstString[ ] = “ Hello ” ; This creates an array and initializes the elements of the array to the individual characters in the string Remember, since we don’t specify a size for the array, the initializing value determines the size Strings have a hidden character at the end that also gets added to the array… So the size of the array for “Hello” is six, not five, as the string termination character – known as a null character – is added

Null character Strings have a special termination character at the end that signifies the end of the string The null character is a backslash followed by a zero. That is, the character looks like: ‘\0’ All strings in C end with this character All arrays holding a string must be declared large enough to hold all the characters as well as the null character.

Declaring and initializing another way Remember how we initialized an integer array with initializers? int iMyFirstArray[ 5 ] = { 0, 0, 0, 0, 0 } ; Can do something similar for strings, using initializers: char cMyFirstString[] = { ‘ H ’, ‘ e ’, ‘ l ’, ‘ l ’, ‘ o ’, ‘ \0 ’ }; This creates an array and initializes the elements of the array to the individual characters (note the null character at the end)

Declaring and Initializing (cont’d) As with integers, if you specify fewer initializing values than you have elements, all the rest are initialized to a value of 0. For examples, char cMyFirstString[ 5 ] = { ‘ A ’ }; would set the zeroth element to ‘A’ and the rest to 0! The string would be “A”, not “A0000” –i.e. the first 0 after the character is read as the null character

Accessing characters in an array Since a string is an array of characters, you can access the individual elements as you would with arrays of integers. For example, with char cMyFirstString[ ] = “ Hello ” ; cMyFirstString[ 0 ] is the character ‘H’ and cMyFirstString[ 2 ] is ‘l’

Printing Character Arrays You can print it the hard way… for ( i = 0; sMyFirstString[ i ] != ‘ \0 ’ ; i++ ) { printf( “ %c ”, sMyFirstString[ i ] ) ; } … or an easier way: printf ( “ %s\n ”, sMyFirstString ); Note the new conversion specifier of %s for string!

Sample Program #1

Reading Input into Character Arrays You can read a string from the keyboard into a character array First, you must create an array large enough to hold your expected input char sInput[ 20 ] ; Then use scanf with the %s specifier scanf( “ %s ”, sInput ); NOTE THERE IS NO & BEFORE THE ARRAY NAME IN scanf ! –It is not needed for arrays! (Please don’t ask me why )

Reading Input cont’d scanf will keep filling the array until you enter a whitespace character It is up to YOU to make sure that your array is large enough for the input If the array is too small for the input, scanf will continue to write beyond the length of the array –will most likely result in run-time errors Remember, you must leave a space for the null character! If you want to enter up to 10 letters, create a string with 11 boxes.

12 #include #define SIZE 10 int main() { char sInput[ SIZE ]; scanf( "%s", sInput); printf("You typed %s\n", sInput); scanf( "%s", sInput); printf("You typed %s\n", sInput); return 0; } Sample Program #2

String (char array) functions You can use certain functions for manipulating strings by including the string.h header file –E.g. #include Useful functions include: –strcpy –strlen –strcmp

strcpy Allows you to copy one string, including the terminating null character, into another. E.g.: char sDestinationString[ 10 ] ; strcpy( sDestinationString, “ Class 1 ” ); Copies “Class 1”, including space, into FIRST parameter, sDestinationString No bounds checking is performed –It is up to YOU to make sure the destination string is large enough (including the null character!)

Modifying arrays in a function It is a peculiarity in C (don’t ask me why ) that by default you can modify the contents of an array passed to the function and its changes will be reflected as well in the block that called the function Any change you make to the elements of an array passed to the function will be propagated upwards –E.g. if we changed the array of student grades within the function, the array would be changed in main! Only works on arrays passed (not individual elements passed)

Some Protection C provides a mechanism with which you can make an array non-modifiable Use the keyword “const” before the declaration in your function definition and prototype. For example: –Protoype: void ArrayPrint( const int [ ], int ); –Definition void ArrayPrint( const int iArrayToPrint[ ], int iElementsCount ) { … }

Many faces of CONST In addition to using const to prevent modifications being propagated upwards, you can use it instead of #define! For example, instead of – #define SIZE 10 You can use –const int SIZE = 10 ;

Strlen Finds the length of a string. Can be created by looking for the \0

strcpy Copies a string The first parameter is the DESTINATION, and the first is the source

strcmp Compares two strings If they are the same word, returns 0, and will be a value other than 0 otherwise