Characters and Strings Functions

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
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.
 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.
 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.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
C Program Design C Characters and Strings 主講人:虞台文.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
© 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.
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).
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
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.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
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,
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C Characters and Strings
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Characters and Strings
C Characters and Strings
Fundamentals of Characters and Strings
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Languages -1 (Introduction to C) strings
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
C Characters and Strings – Review Lab assignments
Functions, Part 2 of 3 Topics Functions That Return a Value
Pointers Call-by-Reference CSCI 230
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
C Characters and Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Characters and Strings Functions Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Functions Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

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 The following slide contains a table of all the functions in <ctype.h>

Character Handling Library

String Conversion Functions In <stdlib.h> (general utilities library) Convert strings of digits to integer and floating-point values

1. Initialize variable 2. Convert string and assign to variable 1 /* Fig. 8.6: fig08_06.c 2 Using atof */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main() 7 { 8 double d; 9 10 d = atof( "99.0" ); 11 printf( "%s%.3f\n%s%.3f\n", 12 "The string \"99.0\" converted to double is ", d, 13 "The converted value divided by 2 is ", 14 d / 2.0 ); 15 return 0; 16 } 1. Initialize variable 2. Convert string and assign to variable 3. Print Program Output The string "99.0" converted to double is 99.000 The converted value divided by 2 is 49.500

Standard Input/Output Library Functions Functions in <stdio.h> Used to manipulate character and string data

3.1 Function definition (note recursion) 1 /* Fig. 8.13: fig08_13.c 2 Using gets and putchar */ 3 #include <stdio.h> 4 5 int main() 6 { 7 char sentence[ 80 ]; 8 void reverse( const char * const ); 9 10 printf( "Enter a line of text:\n" ); 11 gets( sentence ); 12 13 printf( "\nThe line printed backwards is:\n" ); 14 reverse( sentence ); 15 16 return 0; 17 } 18 19 void reverse( const char * const sPtr ) 20 { 21 if ( sPtr[ 0 ] == '\0' ) 22 return; 23 else { 24 reverse( &sPtr[ 1 ] ); 25 putchar( sPtr[ 0 ] ); 26 } 27 } 1. Initialize variables 2. Input 3. Print 3.1 Function definition (note recursion) Program Output reverse calls itself using substrings of the original string. When it reaches the '\0' character it prints using putchar Enter a line of text: Characters and Strings The line printed backwards is: sgnirtS dna sretcarahC

String Manipulation Functions String handling library has functions to Manipulate string data Search strings Tokenize strings Determine string length

1. Initialize variables 2. Function calls 3. Print Program Output 1 /* Fig. 8.19: fig08_19.c 2 Using strcat and strncat */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 char s1[ 20 ] = "Happy "; 9 char s2[] = "New Year "; 10 char s3[ 40 ] = ""; 11 12 printf( "s1 = %s\ns2 = %s\n", s1, s2 ); 13 printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) ); 14 printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) ); 15 printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) ); 16 return 0; 17 } 1. Initialize variables 2. Function calls 3. Print Program Output s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year

Comparison Functions 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 if s1 < s2, zero if s1 == s2 or a positive number if 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

Search Functions of the String Handling Library

Other 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 1 /* Fig. 8.37: fig08_37.c 2 Using strerror */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 printf( "%s\n", strerror( 2 ) ); 9 return 0; 10 } 1. Function call 2. Print Program Output No such file or directory