27
1 Chapter 10 Strings and Pointers

Chapter 10 Strings and Pointers

Embed Size (px)

DESCRIPTION

Chapter 10 Strings and Pointers. Introduction. String Constant Example: printf(“Hello”); “Hello” : a string constant A string constant is a series of characters surrounded by double quotes. How to declare a variable to store string values? - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 10 Strings and Pointers

1

Chapter 10 Strings and Pointers

Page 2: Chapter 10 Strings and Pointers

2

Introduction

String Constant Example: printf(“Hello”);

“Hello” : a string constant o A 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 charchar string[size];

Question: The size of a character array is fixed, how can this variable take string constants with different lengths as values?

Page 3: Chapter 10 Strings and Pointers

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

Page 4: Chapter 10 Strings and Pointers

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.

Page 5: Chapter 10 Strings and Pointers

5

The End-of-String Sentinel \0

Example:

#include <stdio.h>int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; printf("%s\n", w);}

% a.outABC

#include <stdio.h>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

Page 6: Chapter 10 Strings and Pointers

6

The End-of-String Sentinel \0

Example:

#include <stdio.h>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.

Page 7: Chapter 10 Strings and Pointers

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

Page 8: Chapter 10 Strings and Pointers

8

Using scanf to reading string

Using scanf to read in a string scanf(“%s”, w);

read in non-white space characters o positions the input stream to an initial non-

white space charactero read in non-white space characterso The process stops when a white space

character or EOF is encountered.

a null character is placed in memory to end the string.

Page 9: Chapter 10 Strings and Pointers

9

Using scanf to reading string

#include <stdio.h>int main(void){ char w[10]; printf("Enter strings\n", w); scanf("%s", w); printf("%s\n", w);}% a.outEnter stringsHelloHello

% a.outEnter stringsHello WorldHello

scanf(”%s”,w);

read in non-white space characters

positions the input stream to an initial non-white space characterread in non-white space charactersThe process stops when a white space character or EOF is encountered.

a null character is placed in memory to end the string.

Page 10: Chapter 10 Strings and Pointers

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

Page 11: Chapter 10 Strings and Pointers

11

Initialization of Strings

Initialization of Strings Example: initialize a string variable as “abc”

char s[] = {‘a’, ‘b’, ‘c’, ‘\0’};char s[]=“abc”;

#include <stdio.h>int main(void){ char w[]="abc"; printf("%d\n", sizeof(w));}

% a.out4

The size of the string must include the storage needed for the null character \0.

Page 12: Chapter 10 Strings and Pointers

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 <stdio.h>int main(void){ char *p="abc"; printf("%s\n",p);}

% a.outabc

Page 13: Chapter 10 Strings and Pointers

13

Initialization of Strings

Difference between initializing an array with a constant string and initializing a pointer with a constant string

#include <stdio.h>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.outabcdefgabcdefg48

Page 14: Chapter 10 Strings and Pointers

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 stringA string constant is stored in memory by

the compiler.the pointer is assigned the address of the

constant string in memory.

Page 15: Chapter 10 Strings and Pointers

15

Examples: Process a string using array notation with subscripts

#include <stdio.h>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.outEnter a Message:Have a Good Day!

hAVE A gOOD dAY!

Page 16: Chapter 10 Strings and Pointers

16

Examples: Process a string using pointer

#include <stdio.h>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.outEnter a Message:Have a Good Day!

hAVE A gOOD dAY!

Page 17: Chapter 10 Strings and Pointers

17

Examples: Process a string using pointer

#include <stdio.h>int main(void){ char s[] = "Hello World"; printf("%s\n", s); printf("%s\n", s+1); printf("%s\n", s+2);}

% a.outHello Worldello Worldllo World

Page 18: Chapter 10 Strings and Pointers

18

Outline

String:String-Handling Functions in the

Standard LibraryPassing Arguments to main() using an

array of strings

Page 19: Chapter 10 Strings and Pointers

19

String-Handling Functions in the Standard Library

String-handling functions: Function prototypes are provided by string.h

#include <string.h> 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);

Page 20: Chapter 10 Strings and Pointers

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 <stdio.h>#include <string.h>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 Worlds2=Hello World, s1= Good DayHello WorldWorld

Page 21: Chapter 10 Strings and Pointers

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 <stdio.h>#include <string.h>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-101

Page 22: Chapter 10 Strings and Pointers

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 <stdio.h>#include <string.h>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.outs2=Hello World, s1= Hello Worlds2=Hello World, s1= HHello Worlds2=Hello World, s1= HWorld

Page 23: Chapter 10 Strings and Pointers

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 <stdio.h>#include <string.h>int main(void){ char s1[100] = "Good Day"; printf("%d\n", strlen(s1));} % a.out

8

Page 24: Chapter 10 Strings and Pointers

24

Outline

String:String-Handling Functions in the

Standard LibraryPassing Arguments to main() using an

array of strings

Page 25: Chapter 10 Strings and Pointers

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

Page 26: Chapter 10 Strings and Pointers

26

Passing Arguments to main()

#include <stdio.h>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 World3a.outHelloWorld

Page 27: Chapter 10 Strings and Pointers

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