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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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 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
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
 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.
Topic 10 - Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Introduction to C Programming CE Lecture 13 Strings in C.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Computer Science 210 Computer Organization Strings in C.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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’};
1 Data Structures and Algorithms Programs. Different problems. Operations. Size of data. Resources available.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
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.
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.
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.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
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).
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
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.
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.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages -1 (Introduction to C) strings
CSE 303 Lecture 14 Strings in C
Arrays in C.
CS111 Computer Programming
CSI 121 Structured Programming Language Lecture 21 Character Strings
Lecture 11 Strings.
Beginning C for Engineers
Introduction to Problem Solving and Programming
Presentation transcript:

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

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

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

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

5 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’

6 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

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

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

9 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 (&)!

10 String Input/Output (Alternate functions) #include #define MAXLENGTH 15 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; gets(string1); gets(string2); puts(string1); puts(string2); return 0; }

11 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

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

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

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

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

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”};

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

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

19 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);

20 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

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

22 #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:

23 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:

24 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!”

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

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

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

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

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

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

31 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 “

32 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

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

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

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

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

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

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

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

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

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

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.

43 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 Ali

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 #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } name user Ali\0

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 #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

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 #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

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!

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 Example sscanf(“ hello”,”%d%lf%s”,&n,&val, word);

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