Preview

A First Book of Ansi C Fourth Edition Chapter 9 Character Strings

Good Essays
Open Document
Open Document
1527 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
A First Book of Ansi C Fourth Edition Chapter 9 Character Strings
A First Book of ANSI C
Fourth Edition

Chapter 9 Character Strings

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

A First Book of ANSI C, Fourth Edition

2

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

A First Book of ANSI C, Fourth Edition

3

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

A First Book of ANSI C, Fourth Edition

4

String Input and Output (continued)

A First Book of ANSI C, Fourth Edition

5

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.
A First Book of ANSI C, Fourth Edition 6

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
A First Book of ANSI C, Fourth Edition 7

String Input and Output (continued)

A First Book of ANSI C, Fourth Edition

8

String Processing

You May Also Find These Documents Helpful