# 1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering

## Presentation on theme: "1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering"— Presentation transcript:

1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering bekir.karlik@yasar.edu.tr

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

3 On the Nature of Strings Recall: Main memory –contiguous array of cells –each cell has an address 0x1FFF0x20000x20010x20020x1FFE etc.

4 ch On the Nature of Strings (cont.) Recall: Variable declaration –sets aside a “box” to contain a value Example: char ch; ch = ‘B’; 0x1FFF0x20000x20010x20020x1FFE etc. ‘B’

5 On the Nature of Strings (cont.) Example: char name[5]; Specifies number of cells in the array String declaration –sets aside an array of cells –each cell contains a char –address of first cell in the array

6 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

7 Character Strings Declaration 1: char name[5]; Declaration 2: #define MAXLENGTH 5 char name[MAXLENGTH]; 0x2000 0x2004 name is 0x2000

8 String Input/Output #include #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 (&)!

9 Character Strings Initialization: char name[5] = “Ali”; Ali\0 Terminating Character: Marks the end of string Special char: ’\0’ aka NUL (single L) 0x2000 0x2004 name is 0x2000

10 Character Strings Initialization: char name[5] = “Ali”; Can store at most 4 letters, because of `\0’ Ali\0 0x2000 0x2004 name is 0x2000

11 Character Strings Declaration 3: char name[] = “Ali”; Takes up an extra cell for ‘\0’ Ali\0 0x2000 0x2003 name is 0x2000

12 Character Strings Declaration 4: char *name = “Ali”; Result is “undefined” if you try to modify this string. Ali\0 0x3000 0x3003 0x3000 name

13 Character Strings Declaration 5: char name[]; String with arbitrary length? No! Will cause an error.

14 A Char in a String The size of a character string is fixed. Character at position index: –string[index] –first character has index 0

15 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 h. index 0index 4 Aise\0 0x39950x399C name is 0x3995

16 A Char in a String index 2 Aise\0 0x39950x399C name is 0x3995 char name[8] = “Aise”; name[2] = ‘X’; printf(“Name: %s\n”, name);

17 AiXe\0 0x39950x399C 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

18 String Operations #include Operations: –Assignment: strcpy() –Concatenation: strcat() –Comparison: strcmp() –Length: strlen()

19 #include #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } String Operation: Assignment string1: string2:

20 String Operation: Assignment #include #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } string1: “Hello World!” string2:

21 String Operation: Assignment #include #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!”

22 char name1[5] = “Ali”; char name2[5] = “Sami”; name2 = name1; Common Mistake: Wrong Assignment Example 1: Error: “LValue required....”

23 char *name1 = “Ali”; char *name2 = “Sami”; name2 = name1; Common Mistake: Bad Assignment Example 2: Better avoid initialising strings this way. (Usually, no error message.)

24 Common Mistake: Bad Assignment Sami\0 0x39900x3994 Ali\0 0x20000x2003 0x2000 name1 0x3990 name2 char *name1 = “Ali”; char *name2 = “Sami”;

25 Common Mistake: Bad Assignment Sami\0 0x39900x3994 Ali\0 0x20000x2003 0x2000 name1 0x2000 name2 name2 = name1;

26 Common Mistake: Not enough space Ali\0 0x2000 0x2003 char name[] = “Ali”; strcpy(name, “Samir”); name is 0x2000

27 Common Mistake: Not enough space Samir\0 0x2000 0x2003 char name[] = “Ali”; strcpy(name, “Samir”); Requires caution. name is 0x2000

28 String Operation: Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, “World!”); string1: “Goodbye” string2: “, Cruel “

29 char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, “World!”); string1: “Goodbye, Cruel ” string2: “, Cruel “ String Operation: Concatenation

30 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, “World!”);

31 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, “World!”);

32 Common Mistake: char name[5]; strcpy(name, “Ali”); strcat(name, “ Osman”); Not enough space Ali\0 0x2000 0x2004 name is 0x2000

33 Common Mistake: char name[5]; strcpy(name, “Ali”); strcat(name, “ Osman”); Not enough space AliOsma 0x2000 0x2004 n\0 name is 0x2000

34 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

35 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

36 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

37 output: 5 Number of char’s before the `\0’. char string1[100]; strcpy(string1, “Apple”); printf(“%d\n”, strlen(string1)); String Operation: Length

38 Common Mistake: char name[5]; strcpy(name, “Bekir”); Not enough space Don’t forget the ‘\0’. Bekir\0 0x39900x3994 name is 0x3990

39 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.

40 Example: hello3.c #include #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 Bekir\0

41 int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } Example: hello3.c #include #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } name user Bekir\0

42 int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } Example: hello3.c #include #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } user Bekir! How are you?\0 name

43 int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } Example: hello3.c #include #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } user Bekir! How are you?\0

44 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!

45 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 strings.

Download ppt "1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering"

Similar presentations