CSI 121 Structured Programming Language Lecture 21 Character Strings

Slides:



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

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.
1 CSE1303 Part A Data Structures and Algorithms Semester 2, 2006 Lecture A1 – Welcome & Revision.
1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
Computer Science 210 Computer Organization Strings in C.
Programming Languages -1 (Introduction to C) strings Instructor: M.Fatih AMASYALI
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 and Algorithms Programs. Different problems. Operations. Size of data. Resources available.
Kymberly Fergusson WELCOME CSE1303 Part A Data Structures and Algorithms Summer 2002.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
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.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Computer Organization and Design Pointers, Arrays and Strings in C
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
DEPARTMENT OF COMPUTER SCIENCE & APPLICATION
C Scope Rules and Arrays
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages -1 (Introduction to C) strings
CSE 303 Lecture 14 Strings in C
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
C programming language
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Arrays in C.
Programming Languages and Paradigms
CSI-121 Structured Programming Language Lecture 16 Pointers
Character Strings Lesson Outline
Computer Science 210 Computer Organization
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS111 Computer Programming
Object Oriented Programming COP3330 / CGS5409
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
Strings.
INC 161 , CPE 100 Computer Programming
Pointers and Pointer-Based Strings
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Lecture 11 Strings.
Beginning C for Engineers
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
EECE.2160 ECE Application Programming
Exercise Arrays.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings in C Array of characters is called a string.
Lecture 19: Working with strings
Strings #include <stdio.h>
Programming Languages and Paradigms
Characters and Strings Functions
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Introduction to Problem Solving and Programming
Presentation transcript:

CSI 121 Structured Programming Language Lecture 21 Character Strings CSE1301 Sem 2-2003 CSI 121 Structured Programming Language Lecture 21 Character Strings Lecture 20: Character Strings

Topics Representation Declaration Index of a char in a string CSE1301 Sem 2-2003 Topics Representation Declaration Index of a char in a string String operations Common mistakes Lecture 20: Character Strings

Representation Recall: Main memory contiguous array of cells each cell has an address 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc

Representation (cont) Recall: Variable declaration sets aside a “box” to contain a value Example: char ch; ch = ‘B’; 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc ‘B’ ch

Representation (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]; Specifies number of cells in the array

Representation (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]; name is 0x2000 0x2000 0x2004

Character Arrays vs Character Strings CSE1301 Sem 2-2003 Character Arrays vs Character Strings A character string is a char array A character string must have the terminating character (’\0’) The terminating character allows scanf() and printf() to handle character strings Lecture 20: Character Strings

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

String Input/Output No ampersand (&)! #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; }

Character String Declaration Terminating Character: Marks the end of string Special char: ’\0’ aka NUL (single L) Declaration 1: char name[5] = “Ann”; name is 0x2000 A n n \0 0x2000 0x2004

Character String Declaration char name[5] = “Ann”; Could have defined this as an array: name is 0x2000 char name[5] = {’A’,’n’,’n’,’\0’}; A n n \0 0x2000 0x2004

Character String Declaration (cont) Can store at most 4 letters, because of `\0’ Declaration 1: char name[5] = “Ann”; name is 0x2000 A n n \0 0x2000 0x2004

Character String Declaration (cont) Takes up an extra cell for ‘\0’ Declaration 2: char name[] = “Ann”; name is 0x2000 A n n \0 0x2000 0x2003

Character String Declaration (cont) CSE1301 Sem 2-2003 Character String Declaration (cont) Result is “undefined” if you try to modify this string Declaration 3: char *name = “Ann”; 0x3000 A n n \0 name 0x3000 0x3003 Lecture 20: Character Strings

Character String Declaration (cont) char name[]; String with arbitrary length? No! Will cause an error “array size missing in `name’”

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

A Char in a String (cont) index 0 index 4 J o h n \0 0x3995 0x399C name is 0x3995 char name[8] = “John”; int i = 2; printf(“Char at index %d is %c.\n”, i, name[i]); output: Char at index 2 is h.

A Char in a String (cont) index 2 J o h n \0 0x3995 0x399C name is 0x3995 X char name[8] = “John”; name[2] = ‘X’; printf(“Name: %s\n”, name);

A Char in a String (cont) 0x3995 0x399C name is 0x3995 J o X n \0 index 2 char name[8] = “John”; name[2] = ‘X’; printf(“Name: %s\n”, name); output: Name: JoXn

String Operations #include <string.h> Operations: Assignment: strcpy() Concatenation: strcat() Comparison: strcmp() Length: strlen() All relay on and maintain the NUL termination of the strings.

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: <garbage> string2: <garbage>

String Operation: Assignment (cont) #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>

String Operation: Assignment (cont) #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!”

Common Mistake 1: Incompatible types Example: char name1[5] = “Ann”; CSE1301 Sem 2-2003 Common Mistake 1: Incompatible types Example: char name1[5] = “Ann”; char name2[5] = “Dave”; name2 = name1; Error: “LValue required ...” Lecture 20: Character Strings

Common Mistake 2: Not enough space char name[] = “Ann”; strcpy(name, “David”); name is 0x2000 A n n \0 0x2000 0x2003

Common Mistake 2: Not enough space char name[] = “Ann”; strcpy(name, “David”); name is 0x2000 D a v i d \0 0x2000 0x2003

Caution 1: Pointer Assignment Example: char *name1 = “Ann”; char *name2 = “Dave”; name2 = name1;

Caution 1: Pointer Assignment char *name1 = “Ann”; char *name2 = “Dave”; D a v e \0 0x3990 0x3994 A n 0x2000 0x2003 name1 name2

Caution 1: Pointer Assignment name2 = name1; A n n \0 name1 D a v e \0 0x2000 A n n \0 name1 0x2000 0x2003 0x2000 D a v e \0 name2 0x3990 0x3994

Example: strassign.c #include <stdio.h> CSE1301 Sem 2-2003 Example: strassign.c #include <stdio.h> #include <string.h> #define MAXLENGTH 5 int main() { char name1[MAXLENGTH] = “Ann”; char name2[] = “Ann”; char *name3 = “John”; char name4[MAXLENGTH]; printf(“\nBEFORE\nname1=%s, name2=%s, name3=%s”, name1, name2, name3); strcpy(name1,”Fred”); strcpy(name2,”Fred”); strcpy(name4,name1); name3 = name2; printf(“\nAFTER\nname1=%s, name2=%s, name3=%s, name4=%s”, name1, name2, name3, name4); strcpy(name1,”Jack”); strcpy(name2,”Jim”); printf(“\nLAST\nname1=%s, name2=%s, name3=%s, name4=%s”, return 0; } Lecture 20: Character Strings

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 “

String Operation: Concatenation (cont) 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 (cont) char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, “World!”); string1: “Goodbye, Cruel , Cruel ” string2: “, Cruel ”

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

Common Mistake: Not enough space char name[5]; strcpy(name, “Ann”); strcat(name, “ Smith”); name is 0x2000 A n n \0 0x2000 0x2004

Common Mistake: Not enough space char name[5]; strcpy(name, “Ann”); strcat(name, “ Smith”); name is 0x2000 A n n S m i t h \0 0x2000 0x2004

String Operation: Comparison strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (strcmp(string1, string2) < 0) { printf(“%s %s\n”, string1, string2); } else printf(“%s %s\n”, string2, string1); Returns: negative if string1 < string2 zero if string1 == string2 positive if string1 > string2

String Operation: Comparison (cont) 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

Common Mistake: Wrong Comparison strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (string1 < string2) { printf(“%s %s\n”, string1, string2); } else printf(“%s %s\n”, string2, string1);

Returns zero if the strings are the same. Caution 1: Not a Boolean strcpy(string1, “Hi Mum”); strcpy(string2, “Hi Mum”); if ( strcmp(string1, string2) ) { printf(“%s and %s are the same\n”, string1, string2); } Returns zero if the strings are the same.

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

Common Mistake: Not enough space Don’t forget the ‘\0’ char name[5]; strcpy(name, “David”); Don’t forget the ‘\0’ D a v i d \0 0x3990 0x3994 name is 0x3990

Character Strings as Parameters CSE1301 Sem 2-2003 Character Strings as Parameters Strings as formal parameters are declared as char* or char[] Examples: void Greet ( char* name ) void Greet ( char name[] ) They point to the first element of the string (array of chars) Changes to the string inside the function affect the actual string Lecture 20: Character Strings

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

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

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

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

More of scanf demystified No ampersand (&) in scanf with strings! int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; }