1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege...

49
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering [email protected]

Transcript of 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege...

Page 1: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

1

Introduction to Computing Lecture 11

Character Strings

Assist.Prof.Dr. Nükhet ÖZBEKEge University

Department of Electrical&Electronics [email protected]

Page 2: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

2

Topics

• Strings– Representation – Declaration – Functions – Common mistakes– Index of a char in a string

Page 3: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

3

Introduction

• Grouping of characters is called a string

• As C implements the strings using arrays of type char, we could not explore string until we had dealt with arrays

• Strings are important, because we also have to manipulate textual data as well as numerical data

Page 4: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

4

On the Nature of Strings• Recall: Main memory

– contiguous array of cells– each cell has an address

0x1FFF 0x2000 0x2001 0x20020x1FFEetc.

Page 5: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

5ch

On the Nature of Strings (cont.)• Recall: Variable declaration

– sets aside a “box” to contain a value

Example: char ch;

ch = ‘B’;

0x1FFF 0x2000 0x2001 0x20020x1FFEetc.

‘B’

Page 6: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

6

On the Nature of Strings (cont.)

Example: char name[5];

Specifies numberof cells in the array

• String declaration– sets aside an array of cells– each cell contains a char– address of first cell in the array

Page 7: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

7

On the Nature of Strings (cont.)• String declaration

– sets aside an array of cells– each cell contains a char– address of first cell in the array

Example: char name[5];

0x2000 0x2004

name

is 0x2000

Page 8: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

8

Character StringsDeclaration 1:

char name[5];

Declaration 2:

#define MAXLENGTH 5

char name[MAXLENGTH];

0x2000 0x2004

name

is 0x2000

Page 9: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

9

String Input/Output

#include <stdio.h>

#define MAXLENGTH 15

int main(){ char string1[MAXLENGTH]; char string2[MAXLENGTH];

scanf("%s %s", string1, string2); printf("%s %s\n", string1, string2);

return 0;}

No ampersand (&)!

Page 10: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

10

String Input/Output (Alternate functions)

#include <stdio.h>

#define MAXLENGTH 15

int main(){ char string1[MAXLENGTH]; char string2[MAXLENGTH];

gets(string1); gets(string2); puts(string1); puts(string2); return 0;}

Page 11: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

11

Character Strings

Initialization:

char name[5] = “Ali”;

A l i \0

Terminating Character:• Marks the end of string• Special char: ’\0’ • aka NUL (single L)

0x2000 0x2004

name

is 0x2000

Page 12: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

12

Character Strings

Initialization:

char name[5] = “Ali”;

Can storeat most 4 letters,because of `\0’

A l i \0

0x2000 0x2004

name

is 0x2000

Page 13: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

13

Character Strings

Declaration 3:

char name[] = “Ali”;

Takes up an extra cell for ‘\0’

A l i \0

0x2000 0x2003

name

is 0x2000

Page 14: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

14

Character Strings

Declaration 4:

char *name = “Ali”;

Result is “undefined”if you try to modify this

string.

A l i \0

0x3000 0x3003

0x3000

name

Page 15: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

15

Character Strings

Declaration 5:

char name[];

String with arbitrary length?

No! Will cause an error.

Page 16: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

16

Arrays of Strings

• One string is an array of characters, so an array of strings is a two-dimensional array of characters in which each row is a string

• Example– char days[7][10] = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

Page 17: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

17

A Char in a String

• The size of a character string is fixed.

• Character at position index: –string[index]– first character has index 0

Page 18: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

18

char name[8] = “Aise”;int i = 2;

printf(“Char at index %d is %c.\n”, i, name[i]);

A Char in a String

output: Char at index 2 is s.

index 0 index 4

A i s e \0

0x3995 0x399C

name

is 0x3995

Page 19: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

19

A Char in a String

index 2

A i s e \0

0x3995 0x399C

name

is 0x3995

char name[8] = “Aise”;

name[2] = ‘X’;printf(“Name: %s\n”, name);

Page 20: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

20

A i X e \0

0x3995 0x399C

name

is 0x3995

output: Name: AiXe

index 2

char name[8] = “Aise”;

name[2] = ‘X’;printf(“Name: %s\n”, name);

A Char in a String

Page 21: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

21

String Operations

• #include <string.h> • Operations:

– Assignment: strcpy()– Concatenation: strcat()– Comparison: strcmp()– Length: strlen()

Page 22: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

22

#include <stdio.h>#include <string.h>

#define MAXLENGTH 100

int main(){ char string1[MAXLENGTH]; char string2[MAXLENGTH];

strcpy(string1, “Hello World!”); strcpy(string2, string1);

return 0;}

String Operation: Assignment

string1: <garbage>string2: <garbage>

Page 23: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

23

String Operation: Assignment#include <stdio.h>#include <string.h>

#define MAXLENGTH 100

int main(){ char string1[MAXLENGTH]; char string2[MAXLENGTH];

strcpy(string1, “Hello World!”); strcpy(string2, string1);

return 0;}string1: “Hello World!”string2: <garbage>

Page 24: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

24

String Operation: Assignment#include <stdio.h>#include <string.h>

#define MAXLENGTH 100

int main(){ char string1[MAXLENGTH]; char string2[MAXLENGTH];

strcpy(string1, “Hello World!”); strcpy(string2, string1);

return 0;}

string1: “Hello World!”string2: “Hello World!”

Page 25: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

25

char name1[5] = “Ali”;char name2[5] = “Sami”;

name2 = name1;

Common Mistake:

Wrong Assignment

Example 1:

Error: “LValue required....”

Page 26: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

26

char *name1 = “Ali”;char *name2 = “Sami”;

name2 = name1;

Common Mistake:

Bad Assignment

Example 2:

Better avoid initialising

strings this way.(Usually, no error message.)

Page 27: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

27

Common Mistake:

Bad Assignment

S a m i \0

0x3990 0x3994

A l i \0

0x2000 0x2003

0x2000

name1

0x3990

name2

char *name1 = “Ali”;char *name2 = “Sami”;

Page 28: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

28

Common Mistake:

Bad Assignment

S a m i \0

0x3990 0x3994

A l i \0

0x2000 0x2003

0x2000

name1

0x2000

name2

name2 = name1;

Page 29: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

29

Common Mistake:

Not enough space

A l i \0

0x2000 0x2003

char name[] = “Ali”;

strcpy(name, “Samir”);

name

is 0x2000

Page 30: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

30

Common Mistake:

Not enough space

S a m i r \0

0x2000 0x2003

char name[] = “Ali”;

strcpy(name, “Samir”);

Requires caution.

name

is 0x2000

Page 31: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

31

String Operation: Concatenation

char string1[MAXLENGTH];char string2[MAXLENGTH];

strcpy(string1, “Goodbye”);strcpy(string2, “, Cruel ”);

strcat(string1, string2);strcat(string1, string2);strcat(string1, “World!”);

string1: “Goodbye”string2: “, Cruel “

Page 32: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

32

char string1[MAXLENGTH];char string2[MAXLENGTH];

strcpy(string1, “Goodbye”);strcpy(string2, “, Cruel ”);

strcat(string1, string2);strcat(string1, string2);strcat(string1, “World!”);

string1: “Goodbye, Cruel ”string2: “, Cruel “

String Operation: Concatenation

Page 33: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

33

string1: “Goodbye, Cruel , Cruel ”string2: “, Cruel “

String Operation: Concatenation

char string1[MAXLENGTH];char string2[MAXLENGTH];

strcpy(string1, “Goodbye”);strcpy(string2, “, Cruel ”);

strcat(string1, string2);strcat(string1, string2);strcat(string1, “World!”);

Page 34: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

34

string1: “Goodbye, Cruel , Cruel World!”string2: “, Cruel “

String Operation: Concatenation

char string1[MAXLENGTH];char string2[MAXLENGTH];

strcpy(string1, “Goodbye”);strcpy(string2, “, Cruel ”);

strcat(string1, string2);strcat(string1, string2);strcat(string1, “World!”);

Page 35: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

35

Common Mistake:

char name[5];

strcpy(name, “Ali”);strcat(name, “ Osman”);

Not enough space

A l i \0

0x2000 0x2004

name

is 0x2000

Page 36: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

36

Common Mistake:

char name[5];

strcpy(name, “Ali”);strcat(name, “ Osman”);

Not enough space

A l i O s m a

0x2000 0x2004

n \0name

is 0x2000

Page 37: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

37

strcpy(string1, “Apple”);strcpy(string2, “Wax”);

if (strcmp(string1, string2) < 0){ printf(“%s %s\n”, string1, string2);}else{ printf(“%s %s\n”, string2, string1);}

output: Apple Wax

String Operation: Comparison

Page 38: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

38

strcpy(string1, “Apple”);strcpy(string2, “Wax”);

if (strcmp(string1, string2) < 0){ printf(“%s %s\n”, string1, string2);}else{ printf(“%s %s\n”, string2, string1);}

String Operation: Comparison

Returns:

negative if string1 < string2

zero if string1 == string2

positive if string1 > string2

Page 39: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

39

strcpy(string1, “Apple”);strcpy(string2, “Wax”);

if (string1 < string2){ printf(“%s %s\n”, string1, string2);}else{ printf(“%s %s\n”, string2, string1);}

Common Mistake:

Wrong Comparison

Page 40: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

40

output: 5

Number of char’sbefore the `\0’.

char string1[100];

strcpy(string1, “Apple”);

printf(“%d\n”, strlen(string1));

String Operation: Length

Page 41: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

41

Common Mistake:

char name[5];

strcpy(name, “Osman”);

Not enough space

Don’t forget the ‘\0’.

O s m a n \0

0x3990 0x3994

name

is 0x3990

Page 42: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

42

Character Strings as Parameters

• Strings as formal parameters are declared as char* or char[]– Examples:

void Greet ( char* name )

void Greet ( char name[] )

• As pointer to the first element of the string (array of chars).

• Changes to the string inside the function affect the actual string.

Page 43: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

43

Example: hello3.c

#include <stdio.h>#include <string.h>#define NAMELEN 50

/* Print a simple greeting to the user. */

void Greet ( char * name )

{

strcat(name,"! How are you?");

}

int main(){ char user[NAMELEN];

printf("Who are you? ");

scanf("%s", user);

Greet(user);

printf("%s\n", user);

return 0;}

user

Ali

Page 44: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

44

int main(){ char user[NAMELEN];

printf("Who are you? ");

scanf("%s", user);

Greet(user);

printf("%s\n", user);

return 0;}

Example: hello3.c

#include <stdio.h>#include <string.h>#define NAMELEN 50

/* Print a simple greeting to the user. */

void Greet ( char * name )

{

strcat(name,"! How are you?");

}

name user

Ali\0

Page 45: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

45

int main(){ char user[NAMELEN];

printf("Who are you? ");

scanf("%s", user);

Greet(user);

printf("%s\n", user);

return 0;}

Example: hello3.c

#include <stdio.h>#include <string.h>#define NAMELEN 50

/* Print a simple greeting to the user. */

void Greet ( char * name )

{

strcat(name,"! How are you?");

}

user

Ali! How are you?\0

name

Page 46: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

46

int main(){ char user[NAMELEN];

printf("Who are you? ");

scanf("%s", user);

Greet(user);

printf("%s\n", user);

return 0;}

Example: hello3.c

#include <stdio.h>#include <string.h>#define NAMELEN 50

/* Print a simple greeting to the user. */

void Greet ( char * name )

{

strcat(name,"! How are you?");

}

user

Ali! How are you?\0

Page 47: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

47

int main(){ char user[NAMELEN];

printf("Who are you? ");

scanf("%s", user);

Greet(user);

printf("%s\n", user);

return 0;}

More of scanf demystified

No ampersand (&) in scanf with strings!

Page 48: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

48

sscanf function

• sscanf function works exactly like scanf except that instead of taking the data values for its output parameters from the standard input device, it takes data from the string that is its first argument

• Examplesscanf(“85 96.2 hello”,”%d%lf%s”,&n,&val, word);

Page 49: 1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr.

49

Summary

• A string is a contiguous array of chars• The string identifier is the address of the first char in

the string• Individual chars are accessed using the str[index]

notation• There are C library functions for copying,

concatenating and comparing and finding lengths of strings