Lecture 19: Working with strings

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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.
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 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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.
Characters and Strings
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.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Arithmetic Expressions
C Characters and Strings
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
CSE 303 Concepts and Tools for Software Development
CSE 303 Lecture 14 Strings in C
Strings A string is a sequence of characters treated as a group
Arrays in C.
Chapter 8 - Characters and Strings
CS111 Computer Programming
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
CS 2308 Exam I Review.
INC 161 , CPE 100 Computer Programming
Pointers and Pointer-Based Strings
Strings A collection of characters taken as a set:
C Characters and Strings – Review Lab assignments
String in C++.
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Exercise Arrays.
C++ Programming Lecture 20 Strings
Characters and Strings Functions
Characters and Strings
Characters and Strings
C Characters and Strings
Presentation transcript:

Lecture 19: Working with strings

What Is a String? Importance Used in developing editors, word processors, page layout software, any kinds of text-processing software Strings Series of characters treated as a single unit. Can include letters, digits and various special characters such as +, -, *, /, $, \t, \n. String literal (string constant) in C are written in double quotation marks. “Hello EPSII” In C, a string is an array of characters ending in the null character (‘\0’).

Definition and Initialization Similar to the array definition; The data type of the elements is char. Strings are represented as character arrays ending with '\0' Strintgs char str[ ] = “hello”; char str[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; Arrays int aryInt[ ] = {1, 2, 3, 4, 5}; char aryChar[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ’o’}; Strintgs (#define SIZE 10) char str[SIZE] = “hello”; SIZE > length(“hello”) Arrays (#define SIZE 10) int aryInt[SIZE] = {1, 2, 3, 4, 5}; char aryChar[SIZE] = {‘E’, ‘P’, ‘S’}; The value of a string is the address of its first character. A string is a pointer to the string’s first character. An array is also a constant pointer to its first element.

Definition and Initialization A string can be defined as a variable of type char * const char *colorPtr = “blue”; Create a pointer variable colorPtr that points to the string “blue” somewhere in memory. Inputting strings Using scanf scanf(“%s”, word); Copies input into word[ ] Do not need & (a string evaluates the address of its first character) Remember to leave room in the array for ‘\0’ char word[20]; scanf(“%19s”, word); scanf reads up to 19 characters

Standard Input/Output Library Functions Functions in <stdio.h> Used to manipulate character and string data

Standard Input/Output Library Functions

String Manipulation Functions of the String Handling Library Functions in <string.h> String handling library has functions to Manipulate string data Search strings Tokenize strings Determine string length

String Manipulation Functions of the String Handling Library

String Manipulation Functions of the String Handling Library

Practice Question Q. What is NOT the right way to create a character array and assign it the string “I love EPS II”? char string[25]= “I love EPS II”; char string[25]; strcpy(string, “I love EPS II”; string = “I love EPS II”; char string[25] = {‘I’, ‘ ‘, ‘l’, ‘o’, ‘v’, ‘e’, ‘ ‘, ‘E’, ‘P’, ‘S’, ‘ ‘, ‘I’, ‘I’, ‘\0’}; sprintf(string, “%s”, “I love EPS II”); Solution: C