1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
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.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
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.
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.
1 Review of Chapter 10: String and Pointers. 2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
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.
Chapter 10.
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. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
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.
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Characters and Strings File Processing Exercise C Programming:Part 3.
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)
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
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.
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
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.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
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. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 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.
Arrays and Pointers.
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)
Today’s Material Strings Definition Representation Initialization
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
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.
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.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Lecture 8 String 1. Concept of strings String and pointers
C Programming:Part 3 Characters and Strings File Processing Exercise.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Chapter 8 - Characters and Strings
C Stuff CS 2308.
Pointers and Pointer-Based Strings
C++ Programming Lecture 20 Strings
Introduction to Problem Solving and Programming
Presentation transcript:

1 Chapter 10 Strings and Pointers

2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series of characters surrounded by double quotes.  How to declare a variable to store string values? Represent a string using a one-dimensional array of type char char string[size]; Question: The size of a character array is fixed, how can this variable take string constants with different lengths as values?

3 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

4 The End-of-String Sentinel \0  A string is  a one-dimensional array of type char. char w[100];  character value \0 is used to terminate a string strings have a variable length delimited by the null character \0 but with a maximum length determined by the size of the character array The size of the string must include the storage needed for the null character \0.

5 The End-of-String Sentinel \0  Example: #include int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; printf("%s\n", w); } % a.out ABC #include int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; w[4]=‘D'; printf("%s\n", w); } % a.out ABC the null character value \0 is used to terminate a string

6 The End-of-String Sentinel \0  Example: #include int main(void){ char w[3]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; printf("%s\n", w); } overrun the bounds of w The size of the string must include the storage needed for the null character \0.

7 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

8 Using scanf to reading string  Using scanf to read in a string  scanf(“%s”, w); read in non-white space characters opositions the input stream to an initial non-white space character oread in non-white space characters oThe process stops when a white space character or EOF is encountered. a null character is placed in memory to end the string.

9 Using scanf to reading string #include int main(void){ char w[10]; printf("Enter strings\n", w); scanf("%s", w); printf("%s\n", w); } % a.out Enter strings Hello % a.out Enter strings Hello World Hello scanf(”%s”,w);  read in non-white space characters positions the input stream to an initial non-white space character read in non-white space characters The process stops when a white space character or EOF is encountered.  a null character is placed in memory to end the string.

10 Outline  String:  Representation of a string: \0  Using scanf to reading string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

11 Initialization of Strings  Initialization of Strings  Example: initialize a string variable as “abc” char s[] = {‘a’, ‘b’, ‘c’, ‘\0’}; char s[]=“abc”; #include int main(void){ char w[]="abc"; printf("%d\n", sizeof(w)); } % a.out 4 The size of the string must include the storage needed for the null character \0.

12 Initialization of Strings  A pointer to char can also be initialized with a constant string.  A string constant is stored in memory by the compiler.  the pointer is assigned the address of the constant string in memory.  Example: char p* = “abc”; #include int main(void){ char *p="abc"; printf("%s\n",p); } % a.out abc

13 Initialization of Strings  Difference between  initializing an array with a constant string and  initializing a pointer with a constant string #include int main(void){ char s[]="abcdefg"; char *p="abcdefg"; printf("%s\n",s); printf("%s\n",p); printf("%d\n",sizeof(p)); printf("%d\n",sizeof(s)); } 4 bytes is used to represent a memory address % a.out abcdefg 4 8

14 Initialization of Strings  Difference between  initializing an array with a constant string the array contains the individual characters followed by the null character  initializing a pointer with a constant string A string constant is stored in memory by the compiler. the pointer is assigned the address of the constant string in memory.

15 Examples: Process a string using array notation with subscripts #include int main(void){ char c, name[100]; int i; printf("Enter a Message:\n"); for (i=0; (c=getchar())!='\n'; ++i) name[i]=c; name[i]='\0'; for (i=0; name[i]!='\0'; ++i) { if(isupper(name[i])) name[i]=tolower(name[i]); else if(islower(name[i])) name[i]=toupper(name[i]); } printf("\n%s\n", name); } % a.out Enter a Message: Have a Good Day! hAVE A gOOD dAY!

16 Examples: Process a string using pointer #include int main(void){ char c, name[100], *p; int i; printf("Enter a Message:\n"); for (i=0; (c=getchar())!='\n'; ++i) name[i]=c; name[i]='\0'; for (p=name; *p!='\0'; ++p) { if(isupper(*p)) *p=tolower(*p); else if(islower(*p)) *p=toupper(*p); } printf("\n%s\n", name); } % a.out Enter a Message: Have a Good Day! hAVE A gOOD dAY!

17 Examples: Process a string using pointer #include int main(void){ char s[] = "Hello World"; printf("%s\n", s); printf("%s\n", s+1); printf("%s\n", s+2); } % a.out Hello World ello World llo World

18 Outline  String:  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

19 String-Handling Functions in the Standard Library  String-handling functions:  Function prototypes are provided by string.h #include  Functions: Concatenate two strings: strcat (s1, s2); Compare two strings: int strcmp (s1, s2); Copy s2 to s1: strcpy (s1, s2); Length of a string: strlen (s);

20 String-Handling Functions in the Standard Library  char &strcat (char *s1, const char *s2); Concatenates s1 and s2, the result is put in s1. The string s1 is returned. #include int main(void){ char s1[100] = "Good Day"; char s2[100] = "Hello World"; strcat(s1, s2); printf("s2=%s, s1= %s\n", s2, s1); strcat(s1, s2+6); printf("s2=%s, s1= %s\n", s2, s1); } % a.out s2=Hello World, s1= Good DayHello World s2=Hello World, s1= Good DayHello WorldWorld

21 String-Handling Functions in the Standard Library  int strcmp (const char &s1, const char *s2); An integer is returned that is less than, equal to, or greater tan zero, depending on whether s1 is lexicographically less than, equal to, or greater than s2 #include int main(void){ char s1[100] = "Good Day"; char s2[100] = "Hello World"; printf("%d\n", strcmp(s1, s2)); printf("%d\n", strcmp(s1, s1)); printf("%d\n", strcmp(s2, s1)); } % a.out 0 1

22 String-Handling Functions in the Standard Library  char *strcpy (char *s1, const char *s2); s2 is copied into s1 until \0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The value s1 is returned. #include int main(void){ char s1[100] = "Good Day"; char s2[100] = "Hello World"; strcpy(s1, s2); printf("s2=%s, s1= %s\n", s2, s1); strcpy(s1+1, s2); printf("s2=%s, s1= %s\n", s2, s1); strcpy(s1+1, s2+6); printf("s2=%s, s1= %s\n", s2, s1); } % a.out s2=Hello World, s1= Hello World s2=Hello World, s1= HHello World s2=Hello World, s1= HWorld

23 String-Handling Functions in the Standard Library  unsigned strlen (const char *s); A count of the number of characters before \0 is returned. #include int main(void){ char s1[100] = "Good Day"; printf("%d\n", strlen(s1)); } % a.out 8

24 Outline  String:  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

25 Passing Arguments to main()  How main() communicates with the operating system?  int main(void)  int main( int argc, char *argv[]) argc: the number of the command line arguments argv: an array of strings

26 Passing Arguments to main() #include int main(int argc, char *argv[]){ int i; printf("%d \n", argc); for (i=0; i < argc; ++ i) printf("%s\n", argv[i]); } %a.out Hello World 3 a.out Hello World

27 Summary  String:  Representing a string using an array of characters  \0 is used to terminated a string  strings have a variable length delimited by the null character \0 but with a maximum length determined by the size of the character array  initialization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main()  argc: number of arguments  argv: an array of strings