CNG 140 C Programming (Lecture set 9) Spring 2006-2007 Chapter 9 Character Strings.

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

Chapter 8 Improving the User Interface
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Introduction to C Programming
Chapter 9 Character Strings
Chapter 10.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9 Formatted Input/Output 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. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
A First Book of ANSI C Fourth Edition
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Chapter 15 Strings as Character Arrays
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
A First Book of ANSI C Fourth Edition Chapter 9 Character Strings.
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.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Chapter 9 - Formatted Input/Output
Chapter 3 Assignment and Interactive Input.
Objectives You should be able to describe: Interactive Keyboard Input
A First Book of ANSI C Fourth Edition
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatted Input/Output
Introduction to C++ Programming
A First Book of ANSI C Fourth Edition
Chapter 9 - Formatted Input/Output
Formatted Input/Output
Formatted Input/Output
Presentation transcript:

CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings

CNG 140 C Programming Objectives String Fundamentals Library Functions Input Data Validation Formatting Strings (Optional) Case Study: Character and Word Counting Common Programming and Compiler Errors

CNG 140 C Programming String Fundamentals A string literal is any sequence of characters enclosed in double quotes –"Good Morning!" –Also called: string constant, string value, string –A string is stored as an array of characters terminated by an end-of-string symbolic constant named NULL ( '\0' )

CNG 140 C Programming String Input and Output gets() accepts and stores the characters typed at the terminal into the character array –Pressing the Enter key generates a newline character, \n, which is interpreted by gets() as the end-of-character entry –All the characters encountered by gets(), except the newline character, are stored in the message array

CNG 140 C Programming String Input and Output (continued)

CNG 140 C Programming String Input and Output (continued) Sample run: Enter a string: This is a test input of a string of characters. The string just entered is: This is a test input of a string of characters.

CNG 140 C Programming String Input and Output (continued) A printf() function call can be used in place of a puts() function call –printf("%s\n",message); ≡ puts(message); This correspondence between the output functions is not duplicated by the input functions scanf() and gets() –scanf() reads a set of characters up to either a blank space or a newline character scanf("%s",message); //No & is required –gets() stops accepting characters only when a newline is detected

CNG 140 C Programming String Input and Output (continued)

CNG 140 C Programming String Processing: A primitive string copy example

CNG 140 C Programming String Processing (continued) while (string2[i]) is valid

CNG 140 C Programming NOTE: Because the expression string2[i] is only 0 at the end of a string and non-0 for every other character, the expression while (string2[i] != '\0') can be replaced by the simpler expression while (string2[i]). String Processing (continued)

CNG 140 C Programming String Processing (continued)

CNG 140 C Programming Be careful: omitting the parentheses causes the entire expression to be equivalent to c = (getchar() != '\n') String Processing: char by char input

CNG 140 C Programming String Processing: building higher level function

CNG 140 C Programming Library Functions Note: Attempting to copy a larger string into a smaller string causes the copy to overflow the destination array beginning with the memory area immediately following the last array element.

CNG 140 C Programming Library Functions (continued)

CNG 140 C Programming Library Functions (continued) When comparing strings, their individual characters are evaluated in pairs; if a difference is found, the string with the first lower character is the smaller one –"Good Bye" is less than "Hello" because the first 'G' in Good Bye is less than the first 'H' in Hello –"Hello" is less than "Hello " because the '\0' terminating the first string is less than the ' ' in the second string –"123" is greater than "122" because '3' in 123 is greater than '2' in 122 –"1237" is greater than "123" because '7' in 1237 is greater than '\0' in 123

CNG 140 C Programming Library Functions: use of string functions

CNG 140 C Programming Library Functions (continued) Sample output: Hello is less than Hello there The length of string1 is 5 characters The length of string2 is 11 characters After concatenation, string1 contains the string value Hello there World! The length of this string is 18 characters Type in a sequence of characters for string2: It's a wonderful day After copying string2 to string1, the string value in string1 is: It's a wonderful day The length of this string is 20 characters The starting address of the string1 string is:

CNG 140 C Programming Library Functions (continued) Sample output: Hello is less than Hello there The length of string1 is 5 characters The length of string2 is 11 characters After concatenation, string1 contains the string value Hello there World! The length of this string is 18 characters Type in a sequence of characters for string2: It's a wonderful day After copying string2 to string1, the string value in string1 is: It's a wonderful day The length of this string is 20 characters The starting address of the string1 string is:

CNG 140 C Programming Character Routines

CNG 140 C Programming Character Routines (continued)

CNG 140 C Programming Conversion Routines

CNG 140 C Programming Conversion Routines (continued)

CNG 140 C Programming Input Data Validation Successful programs always try to anticipate invalid data and isolate such data from being accepted and processed –First validate that the data is of the correct type; if not, request the user to re-enter the data –Explain why the entered data was invalid One of the most common methods of validating input data is to accept all numbers as strings –Each character can then be checked to ensure that it complies with the data type being requested

CNG 140 C Programming Input Data Validation: valid integer is preceded by – or +

CNG 140 C Programming Input Data Validation: validation function

CNG 140 C Programming Input Data Validation: algorithm We can use isvalidInt() in a loop that continually requests an integer until a valid integer value is entered Set an integer variable named isanInt to 0 do Accept a string value If the string value does not correspond to an integer Display the error message "Invalid integer - Please re-enter: " Send control back to expression being tested by the do-while statement Set isanInt to 1 (this causes the loop to terminate) while(isanInt is 0) Return the integer corresponding to the entered string

CNG 140 C Programming … Input Data Validation: using another function to use validation function

CNG 140 C Programming Creating a Personal Library Programmers create their own libraries of functions –This permits the functions to be incorporated in any program without further expenditure of coding time Each file in a library contains related functions –#include –#include "C:\\mylibrary\\dataChecks.h" The #include statement for dataChecks.h must be placed after the #include statements for the stdio.h and stdlib.h header files (the functions in dataChecks.h require stdio.h and stdlib.h functions to correctly compile)

CNG 140 C Programming Formatting Strings Examples: To right justification –printf("|%25s|","Have a Happy Day"); | Have a Happy Day| To left justify –printf("|%-25s|","Have a Happy Day"); |Have a Happy Day To specify the max number characters in a given field | –printf("|%25.12s|","Have a Happy Day"); | Have a Happy| To print a max number of char –printf("|%.12s|","Have a Happy Day"); |Have a Happy|

CNG 140 C Programming In-Memory String Conversions The sprintf() and sscanf() functions provide capabilities for writing and scanning strings to and from memory variables –sprintf(disStrn,"%d %d", num1, num2); –sscanf(data,"%c%lf %d",&dol,&price,&units); "$ " –sscanf(date,"%d/%d/%d", &month, &day, &year); "07/01/94"

CNG 140 C Programming Format Strings The control string containing the conversion control sequences need not be explicitly contained within the function –printf("$%5.2f %d",num1,num2); –Or, char fmat[] = "$%5.2f %d"; printf(fmat,num1,num2); The control format strings can be initialized at the beginning of a function

CNG 140 C Programming Case Study: Character and Word Counting 1.We construct a function that counts the number of characters in a string 2.We construct a function that counts the number of words in a sting

CNG 140 C Programming Program Requirement: Character Counting Pass a string to a function and have the function return the number of characters in the string Any character in the string (blank, printable, or nonprintable character) is to be counted The end-of-string NULL character is not to be included in the final count

CNG 140 C Programming Analyze the Problem Determine the input data Determine the required outputs List the algorithm(s) relating the inputs to the outputs

CNG 140 C Programming Analyze the Problem (continued)

CNG 140 C Programming Code the Function int countchar(char list[]) { int i, count = 0; for(i = 0; list[i] != '\0'; i++) count++; return(count); }

CNG 140 C Programming Test the Function …

CNG 140 C Programming Requirement Specification: Word Counting The last word does not have a trailing blank More than one blank may be used between words Leading blanks may be used before the first word

CNG 140 C Programming Analyze the Problem Determine the input data Determine the required outputs Algorithm: Set an integer variable named inaword to the symbolic constant NO Set the word count to 0 For all the characters in the array If the current character is a blank set inaword to NO Else if (inaword equals NO) set inaword to the symbolic constant YES increment the word count EndIf EndFor Return the count

CNG 140 C Programming Code the Function int countword(char list[]) #define YES 1 #define NO 0 { int i, inaword, count = 0; inaword = NO; for(i = 0; list[i] != '\0'; i++) { if (list[i] == ' ') inaword = NO; else if (inaword == NO )/*if there is at least one char*/ { inaword = YES; count++; } return(count); }

CNG 140 C Programming Test and Debug the Function …

CNG 140 C Programming Test and Debug the Function (continued) A sample run using Program 9.11 follows: Type in any number of words: This is a test line with a bunch of words The number of words just entered is 10 Further tests that should be performed are –Enter words with multiple spaces between them –Enter words with leading spaces before the first word –Enter words with trailing spaces after the last word –Enter a sentence that ends in a period or question mark

CNG 140 C Programming Common Programming Errors Forgetting the terminating NULL character, '\0', when processing existing strings in a character-by- character manner Forgetting to terminate a newly created character string with the NULL character Forgetting that the newline character, '\n', is a valid data input character Forgetting to include the string.h, ctype.h, and stdlib.h header files when using the string library, character library, and conversion library functions, respectively

CNG 140 C Programming Common Compiler Errors

CNG 140 C Programming Common Compiler Errors (continued)

CNG 140 C Programming Summary A string is an array of characters terminated by the NULL ( '\0' ) character Character arrays can be initialized using a string assignment of the form char arrayName[] = "text"; Strings can always be processed using standard array-processing techniques The gets(), scanf(), and getchar() library functions can be used to input a string The puts(), printf(), and putchar() functions can be used to display strings

CNG 140 C Programming Summary (continued) Many standard library functions exist for processing strings as a complete unit The standard C library also includes individual character-handling functions ( ctype.h) One of the major uses of strings is validating user input, which is an essential part of any program The conversion routines atoi() and atof() are provided in the stdlib.h header file for converting strings to integer and double-precision numeric values