 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.

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

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays 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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - Bits, Characters, Strings and Structures Outline 18.1 Introduction 18.2 Structure Definitions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS Nov 2006 C-strings.
 2006 Pearson Education, Inc. All rights reserved Bits, Characters, C-Strings and struct s.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
 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.
Programming Languages -1 (Introduction to C) strings Instructor: M.Fatih AMASYALI
Introduction to C programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
February 14, 2005 Characters, Strings and the String Class.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Characters and Strings File Processing Exercise C Programming:Part 3.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
Characters, Strings, And The string Class Chapter 10.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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.
Characters and Strings
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,
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.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C Characters and Strings
INC 161 , CPE 100 Computer Programming
C Characters and Strings
Fundamentals of Characters and Strings
Programming Languages -1 (Introduction to C) strings
CSE 303 Lecture 14 Strings in C
C Programming:Part 3 Characters and Strings File Processing Exercise.
Chapter 8 - Characters and Strings
Introduction to Programming
C++ Programming Lecture 20 Strings
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion Functions 8.5Standard Input/Output Library Functions 8.6String Manipulation Functions of the String Handling Library 8.7Comparison Functions of the String Handling Library 8.8Search Functions of the String Handling Library 8.9Memory Functions of the String Handling Library 8.10Other Functions of the String Handling Library

 2000 Prentice Hall, Inc. All rights reserved. 8.1Introduction Introduce some standard library functions –Easy string and character processing –Programs can process characters, strings, lines of text, and blocks of memory These techniques used to make –Word processors –Page layout software –Typesetting programs

 2000 Prentice Hall, Inc. All rights reserved. 8.2Fundamentals of Strings and Characters Characters –Building blocks of programs Every program is a sequence of meaningfully grouped characters –Character constant - an int value represented as a character in single quotes 'z' represents the integer value of z

 2000 Prentice Hall, Inc. All rights reserved. 8.2Fundamentals of Strings and Characters (II) Strings –Series of characters treated as a single unit Can include letters, digits, and certain special characters ( *, /, $ ) –String literal (string constant) - written in double quotes ”Hello” Somewhere in memory –Strings are arrays of characters String a pointer to first character Value of string is the address of first character

 2000 Prentice Hall, Inc. All rights reserved. 8.2Fundamentals of Strings and Characters (III) String declarations –Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; –Remember that strings represented as character arrays end with '\0' color has 5 elements

 2000 Prentice Hall, Inc. All rights reserved. 8.2Fundamentals of Strings and Characters (IV) Inputting strings –Use scanf scanf("%s", word); Copies input into word[], which does not need & (because a string is a pointer) –Remember to leave space for '\0'

 2000 Prentice Hall, Inc. All rights reserved. 8.3Character Handling Library Character Handling Library –Includes functions to perform useful tests and manipulations of character data –Each function receives a character (an int ) or EOF as an argument

 2000 Prentice Hall, Inc. All rights reserved. 8.3 Character Handling Library (II) In

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Load header 2. Perform tests 3. Print 1/* Fig. 8.2: fig08_02.c 2 Using functions isdigit, isalpha, isalnum, and isxdigit */ 3#include 4#include 5 6int main() 7{7{ 8 printf( "%s\n%s%s\n%s%s\n\n", "According to isdigit: ", 9 isdigit( '8' ) ? "8 is a " : "8 is not a ", "digit", 10 isdigit( '#' ) ? "# is a " : 11 "# is not a ", "digit" ); 12 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", 13 "According to isalpha:", 14 isalpha( 'A' ) ? "A is a " : "A is not a ", "letter", 15 isalpha( 'b' ) ? "b is a " : "b is not a ", "letter", 16 isalpha( '&' ) ? "& is a " : "& is not a ", "letter", 17 isalpha( '4' ) ? "4 is a " : 18 "4 is not a ", "letter" ); 19 printf( "%s\n%s%s\n%s%s\n%s%s\n\n", 20 "According to isalnum:", 21 isalnum( 'A' ) ? "A is a " : "A is not a ", 22 "digit or a letter", 23 isalnum( '8' ) ? "8 is a " : "8 is not a ", 24 "digit or a letter", 25 isalnum( '#' ) ? "# is a " : "# is not a ", 26 "digit or a letter" ); 27 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n", 28 "According to isxdigit:", 29 isxdigit( 'F' ) ? "F is a " : "F is not a ", 30 "hexadecimal digit", 31 isxdigit( 'J' ) ? "J is a " : "J is not a ", 32 "hexadecimal digit",

 2000 Prentice Hall, Inc. All rights reserved. Outline Program Output 33 isxdigit( '7' ) ? "7 is a " : "7 is not a ", 34 "hexadecimal digit", 35 isxdigit( '$' ) ? "$ is a " : "$ is not a ", 36 "hexadecimal digit", 37 isxdigit( 'f' ) ? "f is a " : "f is not a ", 38 "hexadecimal digit" ); 39 return 0; 40} According to isdigit: 8 is a digit # is not a digit According to isalpha: A is a letter b is a letter & is not a letter 4 is not a letter According to isalnum: A is a digit or a letter 8 is a digit or a letter # is not a digit or a letter According to isxdigit: F is a hexadecimal digit J is not a hexadecimal digit 7 is a hexadecimal digit $ is not a hexadecimal digit f is a hexadecimal digit

 2000 Prentice Hall, Inc. All rights reserved. /* Fig. 8.3: fig08_03.c Using functions islower, isupper, tolower, toupper */ #include int main() { printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", "According to islower:", islower( 'p' ) ? "p is a " : "p is not a ", "lowercase letter", islower( 'P' ) ? "P is a " : "P is not a ", "lowercase letter", islower( '5' ) ? "5 is a " : "5 is not a ", "lowercase letter", islower( '!' ) ? "! is a " : "! is not a ", "lowercase letter" );

 2000 Prentice Hall, Inc. All rights reserved. printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", "According to isupper:", isupper( 'D' ) ? "D is an " : "D is not an ", "uppercase letter", isupper( 'd' ) ? "d is an " : "d is not an ", "uppercase letter", isupper( '8' ) ? "8 is an " : "8 is not an ", "uppercase letter", isupper( '$' ) ? "$ is an " : "$ is not an ", "uppercase letter" ); printf( "%s%c\n%s%c\n%s%c\n%s%c\n", "u converted to uppercase is ", toupper( 'u' ), "7 converted to uppercase is ", toupper( '7' ), "$ converted to uppercase is ", toupper( '$' ), "L converted to lowercase is ", tolower( 'L' ) ); return 0; /* indicates successful termination */ } /* end main */

 2000 Prentice Hall, Inc. All rights reserved.

/* Fig. 8.4: fig08_04.c Using functions isspace, iscntrl, ispunct, isprint, isgraph */ #include int main() { printf( "%s\n%s%s%s\n%s%s%s\n%s%s\n\n", "According to isspace:", "Newline", isspace( '\n' ) ? " is a " : " is not a ", "whitespace character", "Horizontal tab", isspace( '\t' ) ? " is a " : " is not a ", "whitespace character", isspace( '%' ) ? "% is a " : "% is not a ", "whitespace character" ); printf( "%s\n%s%s%s\n%s%s\n\n", "According to iscntrl:", "Newline", iscntrl( '\n' ) ? " is a " : " is not a ", "control character", iscntrl( '$' ) ? "$ is a " : "$ is not a ", "control character" );

 2000 Prentice Hall, Inc. All rights reserved. printf( "%s\n%s%s\n%s%s\n%s%s\n\n", "According to ispunct:", ispunct( ';' ) ? "; is a " : "; is not a ", "punctuation character", ispunct( 'Y' ) ? "Y is a " : "Y is not a ", "punctuation character", ispunct( '#' ) ? "# is a " : "# is not a ", "punctuation character" ); printf( "%s\n%s%s\n%s%s%s\n\n", "According to isprint:", isprint( '$' ) ? "$ is a " : "$ is not a ", "printing character", "Alert", isprint( '\a' ) ? " is a " : " is not a ", "printing character" ); printf( "%s\n%s%s\n%s%s%s\n", "According to isgraph:", isgraph( 'Q' ) ? "Q is a " : "Q is not a ", "printing character other than a space", "Space", isgraph( ' ' ) ? " is a " : " is not a ", "printing character other than a space" ); return 0; /* indicates successful termination */ } /* end main */

 2000 Prentice Hall, Inc. All rights reserved.

8.4 String Conversion Functions Conversion functions –In (general utilities library) –Convert strings of digits to integer and floating-point values Prototype Description double atof( const char *nPtr ) Converts the string nPtr to double. int atoi( const char *nPtr ) Converts the string nPtr to int. long atol( const char *nPtr ) Converts the string nPtr to long int. double strtod( const char *nPtr, char **endPtr ) Converts the string nPtr to double. long strtol( const char *nPtr, char **endPtr, int base ) Converts the string nPtr to long. unsigned long strtoul( const char *nPtr, char **endPtr, int base ) Converts the string nPtr to unsigned long.

 2000 Prentice Hall, Inc. All rights reserved. /* Fig. 8.6: fig08_06.c Using atof */ #include int main() { double d; /* variable to hold converted string */ d = atof( "99.0" ); printf( "%s%.3f\n%s%.3f\n", "The string \"99.0\" converted to double is ", d, "The converted value divided by 2 is ", d / 2.0 ); return 0; /* indicates successful termination */ } /* end main */

 2000 Prentice Hall, Inc. All rights reserved. Using atoi

 2000 Prentice Hall, Inc. All rights reserved. Using atol

 2000 Prentice Hall, Inc. All rights reserved. strtod Function strtod converts a sequence of characters representing a floating point value to double. The function receives two arguments – a string (char *) and a pointer to a string (char **). The string contains the character sequence to be converted portion of the string.

 2000 Prentice Hall, Inc. All rights reserved. Using strtod

 2000 Prentice Hall, Inc. All rights reserved.

Using strtol

 2000 Prentice Hall, Inc. All rights reserved. Using strtoul

 2000 Prentice Hall, Inc. All rights reserved. 8.5Standard Input/Output Library Functions Functions in –Used to manipulate character and string data Function prototype Function description int getchar( void ); Inputs the next character from the standard input and returns it as an integer. char *gets( char *s ); Inputs characters from the standard input into the array s until a newline or end-of- file character is encountered. A terminating null character is appended to the array. int putchar( int c ); Prints the character stored in c. int puts( const char *s ); Prints the string s followed by a newline character. int sprintf( char *s, const char *format,... ); Equivalent to printf, except the output is stored in the array s instead of printing it on the screen. int sscanf( char *s, const char *format,... ); Equivalent to scanf, except the input is read from the array s instead of reading it from the keyboard.

 2000 Prentice Hall, Inc. All rights reserved. Using gets and putchar a b c d‘\0’ sentence

 2000 Prentice Hall, Inc. All rights reserved. a b c d‘\0’

 2000 Prentice Hall, Inc. All rights reserved. Using getchar anad puts

 2000 Prentice Hall, Inc. All rights reserved. Result

 2000 Prentice Hall, Inc. All rights reserved. Using sprintf

 2000 Prentice Hall, Inc. All rights reserved. Using sscanf

 2000 Prentice Hall, Inc. All rights reserved. 8.6String Manipulation Functions of the String Handling Library String handling library has functions to –Manipulate string data –Search strings –Tokenize strings –Determine string length

 2000 Prentice Hall, Inc. All rights reserved.

Using strcpy and strncpy

 2000 Prentice Hall, Inc. All rights reserved. Result

 2000 Prentice Hall, Inc. All rights reserved. Using strcat and strncat

 2000 Prentice Hall, Inc. All rights reserved. 8.7Comparison Functions of the String Handling Library Comparing strings –Computer compares numeric ASCII codes of characters in string –Appendix D has a list of character codes int strcmp( const char *s1, const char *s2 ); –Compares string s1 to s2 –Returns a negative number ( s1 s2 ) int strncmp( const char *s1, const char *s2, size_t n ); –Compares up to n characters of string s1 to s2 –Returns values as above

 2000 Prentice Hall, Inc. All rights reserved. Using strcmp and strncmp

 2000 Prentice Hall, Inc. All rights reserved.

8.8Search Functions of the String Handling Library

 2000 Prentice Hall, Inc. All rights reserved. Using strchr

 2000 Prentice Hall, Inc. All rights reserved. strcspn and strpbrk Function strcspn determines the length of the initial part of the string in first argument that does not contain any characters from the string in its second argument. Function strcspn searches for the first occurrence in its first string argument of any character in its second string argument.

 2000 Prentice Hall, Inc. All rights reserved. Using strcspn

 2000 Prentice Hall, Inc. All rights reserved. Using strpbrk

 2000 Prentice Hall, Inc. All rights reserved. strrchr and strspn Function strrchr searches for the last occurrence of the specified character in a string. Function strspn determines the length of the initial part of the string in its first argument that contains only characters from the string in its second argument.

 2000 Prentice Hall, Inc. All rights reserved. Using strrchr

 2000 Prentice Hall, Inc. All rights reserved. Using strspn

 2000 Prentice Hall, Inc. All rights reserved. Strstr and strtok Function strstr searches for the first occurrence of its second string argument is its first string argument. Function strtok is used to break a string into a series of tokens. tokenPtr = strtok ( string, “ “); –indicates that tokens in string are separated by spaces.

 2000 Prentice Hall, Inc. All rights reserved. Using strstr

 2000 Prentice Hall, Inc. All rights reserved. strtok

 2000 Prentice Hall, Inc. All rights reserved. 8.9 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

 2000 Prentice Hall, Inc. All rights reserved. 8.9 Memory Functions of the String- handling Library (II) "Object" refers to a block of data

 2000 Prentice Hall, Inc. All rights reserved. /* Fig. 8.31: fig08_31.c Using memcpy */ #include int main() { char s1[ 17 ]; /* create char array s1 */ char s2[] = "Copy this string"; /* initialize char array s2 */ memcpy( s1, s2, 17 ); printf( "%s\n%s\"%s\"\n", "After s2 is copied into s1 with memcpy,", "s1 contains ", s1 ); return 0; /* indicates successful termination */ } /* end main */

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize variables 2. Function calls 3. Print Program Output 1/* Fig. 8.32: fig08_32.c 2 Using memmove */ 3#include 4#include 5 6int main() 7{7{ 8 char x[] = "Home Sweet Home"; 9 10 printf( "%s%s\n", 11 "The string in array x before memmove is: ", x ); 12 printf( "%s%s\n", 13 "The string in array x after memmove is: ", 14 memmove( x, &x[ 5 ], 10 ) ); return 0; 17} The string in array x before memmove is: Home Sweet Home The string in array x after memmove is: Sweet Home Home

 2000 Prentice Hall, Inc. All rights reserved. /* Fig. 8.33: fig08_33.c Using memcmp */ #include int main() { char s1[] = "ABCDEFG"; /* initialize char array s1 */ char s2[] = "ABCDXYZ"; /* initialize char array s2 */ printf( "%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n", "s1 = ", s1, "s2 = ", s2, "memcmp( s1, s2, 4 ) = ", memcmp( s1, s2, 4 ), "memcmp( s1, s2, 7 ) = ", memcmp( s1, s2, 7 ), "memcmp( s2, s1, 7 ) = ", memcmp( s2, s1, 7 ) ); return 0; /* indicate successful termination */ } /* end main */

 2000 Prentice Hall, Inc. All rights reserved. /* Fig. 8.34: fig08_34.c Using memchr */ #include int main() { const char *s = "This is a string"; /* initialize char pointer */ printf( "%s\'%c\'%s\"%s\"\n", "The remainder of s after character ", 'r', " is found is ", memchr( s, 'r', 16 ) ); return 0; /* indicates successful termination */ } /* end main */

 2000 Prentice Hall, Inc. All rights reserved. /* Fig. 8.35: fig08_35.c Using memset */ #include int main() { char string1[ 15 ] = "BBBBBBBBBBBBBB"; /* initialize string1 */ printf( "string1 = %s\n", string1 ); printf( "string1 after memset = %s\n", memset( string1, 'b', 7 ) ); return 0; /* indicates successful termination */ } /* end main */

 2000 Prentice Hall, Inc. All rights reserved. 8.10Other Functions of the String Handling Library char *strerror( int errornum ); –Creates a system-dependent error message based on errornum –Returns a pointer to the string size_t strlen( const char *s ); –Returns the number of characters (before NULL ) in string s

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Function call 2. Print Program Output 1/* Fig. 8.37: fig08_37.c 2 Using strerror */ 3#include 4#include 5 6int main() 7{7{ 8 printf( "%s\n", strerror( 2 ) ); 9 return 0; 10} No such file or directory

 2000 Prentice Hall, Inc. All rights reserved.