Programming Languages -1 (Introduction to C) strings Instructor: M.Fatih AMASYALI

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
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
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2006 Pearson Education, Inc. All rights reserved Bits, Characters, C-Strings and struct s.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Introduction to C programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
C Program Design C Characters and Strings 主講人:虞台文.
Characters and Strings File Processing Exercise C Programming:Part 3.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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’};
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Chapter 8: Character and String CMPD144: Programming 1.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
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:
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
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).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Strings, Pointers and Tools
Characters and Strings
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Characters and Strings
C Characters and Strings
Fundamentals of Characters and Strings
Programming Languages -1 (Introduction to C) strings
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
CSI 121 Structured Programming Language Lecture 21 Character Strings
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

Programming Languages -1 (Introduction to C) strings Instructor: M.Fatih AMASYALI

2 The Data Type char The data type char can be thought of as either a character or an integer. Typically, a char has a value printf( “%c”, ‘a’ ); /* a is printed */ printf( “%d”, ‘a’ ); /* 97 is printed */ printf( “%c”, 97 ); /* a is printed */ ‘a’ == 97, ‘b’ == 98, …, ‘z’ == 112 ‘A’ == 65, ‘B’ == 66, …, ‘Z’ = 90 ‘0’ == 48, ‘1’ == 49, …, ’9’ == 57 ‘&’ == 38, ‘*’ == 42, …

3 Codes corresponding to characters For use inside in single-quotes, or double- quotes, for instance in passing a string to printf Character Escape Sequence Integer Value Newline \n 10 Backslash (\) \\ 92 Single quote \’ 39 Double quote \” 34 Horizontal tab \t 9 Question Mark \? 63 Null Character \0 0

4 Strings Strings are one-dimensional arrays of type char. Hence, they have type char *. By convention, a string in C is terminated by \0 (null character); thus it needs space equal to the size of string +1 A string constant is treated by the compiler as a pointer; also space in memory is allocated for storing the characters. char *p = “abc”; printf(“%s %s\n”, p, p+1 ); /* prints abc bc */ printf(“%c”,"abc”[1]); /* prints b */ printf(“%c”,*(“abc” + 2)); /* prints c */ CornellUni\

5 Example: “Double” printing #include void new_print( char *s ) { int i; for( i = 0; s[i] != 0; i++ ) { printf( "%c%c", s[i], s[i] ); } void main() { new_print( “Cornell" ); } All of {0, ‘\0’, NULL} are legal.

6 Strings are also char pointers #include void new_print( char *s ) { while (*s) { printf( "%c%c", *s, *s ); s++; } void main() { new_print( “Cornell" ); }

7 Example: “squeeze” function /* squeeze deletes all instances of c from s */ void squeeze( char s[], int c ) { int i, j; for( i = j = 0; s[i] != ‘\0’; i++ ) { if( s[i] != c ) { s[j] = s[i]; j++; } s[j] = ‘\0’; } Usage: char p[]="Cornell"; squeeze( p, 'o'); printf("%s\n",p);

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

9 Character String Declaration Declaration 1: char name[5] = “Ann”; Ann\0 0x2000 0x2004 name is 0x2000

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

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

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

13 Character String Declaration (cont) Declaration 3: char *name = “Ann”; Result is “undefined” if you try to modify this string Ann\0 0x3000 0x3003 0x3000 name

14 Character String Declaration (cont) Declaration 4: char name[]; String with arbitrary length? No! Will cause an error “storage size of k isn’t known”

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

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

17 char name1[5] = “Anne”; char name2[5] = “Dave”; name2 = name1; Common Mistake 1: Example: Error: “ISO C++ forbids assingment of arrays”

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

19 Caution 1: Pointer Assignment Dave\0 0x39900x3994 Ann\0 0x20000x2003 0x2000 name1 0x3990 name2 char *name1 = “Ann”; char *name2 = “Dave”;

20 Caution 1: Pointer Assignment Dave\0 0x39900x3994 Ann\0 0x20000x2003 0x2000 name1 0x2000 name2 name2 = name1;

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

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

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

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

25 Character-handling library functions.

26 isdigit tests if a character is a decimal digit isalpha tests if a character is a letter

27 isdigit tests if a character is a decimal digit or a letter isxdigit tests if a character is a hexadecimal digit

28

29 islower tests if a character is a lowercase letter isupper tests if a character is an uppercase letter

30 toupper and tolower convert letters to upper or lower case

31 String-Conversion Functions Conversion functions –In (general utilities library) Convert strings of digits to integer and floating-point values

32 String-conversion functions of the general utilities library.

33 atof converts a string to a double

34 atoi converts a string to an int

35 strtod converts a piece of a string to a double

36 Standard Input/Output Library Functions Functions in Used to manipulate character and string data

37 Standard input/output library character and string functions.

38 gets reads a line of text from the user

39 putchar prints a single character on the screen

40 puts prints a line of text on the screen getchar reads a single character from the user

41

42 sprintf prints a line of text into an array like printf prints text on the screen

43 sscanf reads a line of text from an array like scanf reads text from the user

44 String-manipulation functions of the string-handling library.

45 Portability Tip Type size_t is a system-dependent synonym for either type unsigned long or type unsigned int.

46 strcpy copies string x into character array y strncpy copies 14 characters of string x into character array z Note that strncpy does not automatically append a null character

47 strcat adds the characters of string s2 to the end of string s1 strncat adds the first 6 characters of string s1 to the end of string s3

48

49 Comparison Functions of the String- Handling Library Comparing strings –Computer compares numeric ASCII codes of characters in string

50 String-comparison functions of the string-handling library.

51 strcmp compares string s1 to string s2

52 strncmp compares the first 6 characters of string s1 to the first X characters of string s3

53 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

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

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

56 String-manipulation functions of the string-handling library.

57 String-manipulation functions of the string-handling library.

58 strchr searches for the first instance of character1 in string

59

60 strcspn returns the length of the initial segment of string1 that does not contain any characters in string2 String1’in, String2’deki karakterlerden hiçbirini içermeyen, ilk kısmının uzunluğu

61 strpbrk returns a pointer to the first appearance in string1 of any character from string2 String1’de, String2’deki karakterlerden herhangi birinin ilk geçtiği yerin adresi

62 strrchr returns the remainder of string1 following the last occurrence of the character c String1’in, c karakterinin son geçtiği yerden itibarenki kısmı

63 strspn returns the length of the initial segment of string1 that contains only characters from string2 String1’in, Sadece String2’deki karakterlerden oluşan ilk kısmının uzunluğu

64 strstr returns the remainder of string1 following the first occurrence of string2 String1’in, string2’nin ilk geçtiği yerden itibarenki kısmı

65 strtok “tokenizes” string by breaking it into tokens at each space Calling strtok again and passing it NULL continues the tokenizing of the previous string

66

67 Memory Functions of the String-Handling Library Memory Functions –In –Manipulate, compare, and search blocks of memory –Can manipulate any block of data Pointer parameters are void * –Any pointer can be assigned to void *, and vice versa –void * cannot be dereferenced Each function receives a size argument specifying the number of bytes (characters) to process

68 Memory functions of the string-handling library.

69 Common Programming Error 8.8 String-manipulation functions other than memmove that copy characters have undefined results when copying takes place between parts of the same string.

70 memcpy copies the first 17 characters from object s2 into object s1

71 memmove copies the first 10 characters from x[5] into object x by means of a temporary array

72 memcmp compares the first 4 characters of objects s1 and s2

73 memchr locates the first occurrence of the character r inside the first 16 characters of object s

74 memset copies the character b into the first 7 characters of object string1

75 Referance Ioannis A. Vetsikas, Lecture notes Dale Roberts, Lecture notes