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.

Slides:



Advertisements
Similar presentations
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Advertisements

Programming and Data Structure
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.
Copyright  Hannu Laine C++-programming Part 5 Strings.
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.
 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.
Week 8 Arrays Part 2 String & Pointer
Introduction to C Programming
 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.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
C Tokens Identifiers Keywords Constants Operators Special symbols.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Programming: Basic Elements of C++.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
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.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
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:
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Pointers *, &, array similarities, functions, sizeof.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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).
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Characters and Strings
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
C Characters and Strings
Characters and Strings
TMF1414 Introduction to Programming
Strings A string is a sequence of characters treated as a group
Arrays in C.
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
CPS120: Introduction to Computer Science
Characters and Strings
C Characters and Strings
Presentation transcript:

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 Array Declaration of Strings Fundamentals of Strings & Characters Initialization of Strings Assigning Values to Strings Calculation of String Size String Conversion Functions Comparison Functions of the Strings ASCII Table

Introduction : What is a String? The data type string is a programmer-defined and it not part of the C language. The C standard library supplies it. The data type string is a programmer-defined and it not part of the C language. The C standard library supplies it. A string with no characters is called a null or empty string. A string with no characters is called a null or empty string. “ ” is the empty string. “ ” is the empty string. Every character in a string has a relative position in the string. Every character in a string has a relative position in the string. The position of the first character is 0, position of the second is 1, and so on. The position of the first character is 0, position of the second is 1, and so on. The length of a string is the number of character in it. The length of a string is the number of character in it. Strings can be treated as array of type char used to store names of people, places, or anything that involves a combination of letters. Strings can be treated as array of type char used to store names of people, places, or anything that involves a combination of letters. However, as number can be stored as characters, a string can be an array of numbers, too. However, as number can be stored as characters, a string can be an array of numbers, too.

KUKUM Sem1-06/07 4EKT120: Computer Programming String Type To use the data type string, the program must include the header file string. To use the data type string, the program must include the header file string. #include #include The statement The statement string info = “Welcome”; declares info to be string variable and also initializes info to “Welcome”. The position of the first character, ‘W’, in the info is 0, the position of the second character, ‘e’, is 1, and so on. The position of the first character, ‘W’, in the info is 0, the position of the second character, ‘e’, is 1, and so on. The variable info is capable of storing (just about) any size string. The variable info is capable of storing (just about) any size string.

KUKUM Sem1-06/07 5EKT120: Computer Programming Character Array (string of characters) char info [10]; char info [10]; Can store “Welcome”, “Good Bye”. Can store “Welcome”, “Good Bye”. char name [50]; char name [50]; Able to store shorter strings that total length. Able to store shorter strings that total length. The last value in the string will be null character (‘\0’). The last value in the string will be null character (‘\0’).

KUKUM Sem1-06/07 6EKT120: Computer Programming Declaration of Strings An example of declaration of an array (or string of characters): An example of declaration of an array (or string of characters): It is not necessary that this max size of 10 characters should at all the times be fully used. info could store at any part of the program either the string of characters “Welcome” or the string “Good Bye”. It is not necessary that this max size of 10 characters should at all the times be fully used. info could store at any part of the program either the string of characters “Welcome” or the string “Good Bye”. info char info [10]; can store a string up to 10 characters long, and may visualize it as below

KUKUM Sem1-06/07 7EKT120: Computer Programming Declaration of Strings (cont..) A null character, constant 0 or ‘\0’ can be written at the end of the valid content of a string if the characters to be stored is shorter strings that its total length (10 in this case). A null character, constant 0 or ‘\0’ can be written at the end of the valid content of a string if the characters to be stored is shorter strings that its total length (10 in this case). info is an array of 10 elements of type char could be represented by storing the strings of characters “Welcome” and “Good Bye” in the following way…………. info is an array of 10 elements of type char could be represented by storing the strings of characters “Welcome” and “Good Bye” in the following way………….

KUKUM Sem1-06/07 8EKT120: Computer Programming Declaration of Strings (cont..) W info elcome\0 GeyBdoO to indicate end of string indefinite values

KUKUM Sem1-06/07 9EKT120: Computer Programming Fundamentals of Strings and Characters String declarations String declarations Declare as a character array or a variable of type char * Declare as a character array or a variable of type char * char info[] = “Welcome"; char *infoPtr = “Welcome"; Remember that strings represented as character arrays end with '\0' Remember that strings represented as character arrays end with '\0' info has 8 elements info has 8 elements Inputting strings Inputting strings Use scanf Use scanf scanf(“ %s ", word); Copies input into word[] Copies input into word[] Do not need & (because a string is a pointer) Do not need & (because a string is a pointer) Remember to leave room in the array for '\0' Remember to leave room in the array for '\0'

KUKUM Sem1-06/07 10EKT120: Computer Programming Initialization of string Same like other array, but each character is enclosed in ‘ ’ or “ ”. Same like other array, but each character is enclosed in ‘ ’ or “ ”. 2 ways of initializing the string as follow…. 2 ways of initializing the string as follow…. char newstring[]={‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘\0’}; char newstring[]={‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘\0’}; char newstring[]= “Welcome”; char newstring[]= “Welcome”; ‘\0’ is automatically inserted. ‘\0’ is automatically inserted. The difference is that single quotes (‘) are used to specify single character constants and null character must be added at the end of the sentence. The difference is that single quotes (‘) are used to specify single character constants and null character must be added at the end of the sentence.

KUKUM Sem1-06/07 11EKT120: Computer Programming Initialization of string (cont…) On the other hand, double quotes (“) are constant that specify sequence of characters and between double quotes have always a null character (‘\0’) automatically inserted at the end. On the other hand, double quotes (“) are constant that specify sequence of characters and between double quotes have always a null character (‘\0’) automatically inserted at the end. char newstring[]= {‘W’,’e’,’l’,’c’,’o’,’m’,’e’,’\0’}; char newstring[] = “Welcome”; Single quotes – null char must be added Double quotes – null char automatically inserted

KUKUM Sem1-06/07 12EKT120: Computer Programming The examples below are not valid for string of characters (array). The examples below are not valid for string of characters (array). newstring = “Welcome”; // no [ ] and data type newstring [] =“Welcome”; // no data type newstring = {‘W’,’e’,’l’,’c’,’o’,’m’,’e’,’\0’}; // no [ ] and data type Initialization of string (cont…)

KUKUM Sem1-06/07 13EKT120: Computer Programming Determining the size of the string sizeof - function to get the size of the variable in term of bytes. sizeof - function to get the size of the variable in term of bytes. - Special unary operator used to determine the size in bytes of an array (or any other data type) during program compilation. Example float array[20] What is the size of the array??? What is the size of the array in bytes????

KUKUM Sem1-06/07 14EKT120: Computer Programming Size = 20 Elements Size in bytes ????????? Variables of type float are normally stored in 4 bytes of memory, and array is defined to have 20 elements. Therefore the size in bytes……. 20 x 4 bytes = 80 bytes Note for following variables char are normally stored in 1 byte of memory char are normally stored in 1 byte of memory double are normally stored in 8 bytes of memory double are normally stored in 8 bytes of memory Etc Etc

KUKUM Sem1-06/07 15EKT120: Computer Programming Programming example char newstring[] = {'W','e','l','c','o','m','e','\0'}; char mystring[] = "Good Bye"; printf ("Size of newstring is %d", sizeof (newstring)); printf ("\nSize of mystring is %d", sizeof (mystring)); Why this this program is written?? What is the output of this program???

KUKUM Sem1-06/07 16EKT120: Computer Programming Output Size of newstring is 8 Size of mystring is 9 Explanation Welcome  char = 1 byte number of alphabets = 7 x 1 byte Good Bye  char = 1 byte number of alphabets = 8 x 1 byte

StringPosition of a Character Length of the String in the String “William Jacob”Position of ‘W’ is 013 Position of the first ‘i’ is 1 Position of ‘ ’ (the space) is 7 Position of ‘J’ is 8 Position of ‘b’ is 12 “Mickey”Position of ‘M’ is 06 Position of ‘i’ is 1 Position of ‘c’ is 2 Position of ‘k’ is 3 Position of ‘e’ is 4 Position of ‘y’ is 5 Determining length of the string

KUKUM Sem1-06/07 18EKT120: Computer Programming strlen - A function to get the length of the string. Programming example char newstring[] = {'W','e','l','c','o','m','e','\0'}; char mystring[] = "Good Bye"; printf ("Size of newstring is %d", sizeof (newstring)); printf ("\nSize of mystring is %d", sizeof (mystring)); printf ("\n\nLength of newstring is %d",strlen(newstring)); printf ("\nLength of mystring is %d", strlen(mystring)); Again…………… Why this this program is written?? What is the output of this program???