C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:

Slides:



Advertisements
Similar presentations
Introduction to C Programming
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.
Lecture 20 Arrays and Strings
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.
Chapter 10.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
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.
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
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Input & Output: Console
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Sahar Mosleh California State University San MarcosPage 1 Character String.
13. 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.
 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)
Strings, Slide Fundamental Programming Strings.
Characters and Strings
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
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.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
C Stuff CS 2308.
Strings A collection of characters taken as a set:
CPS120: Introduction to Computer Science
C++ Programming Lecture 20 Strings
Presentation transcript:

C Strings

The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples: const char ASTERISK = '*'; char ch; char letter; letter = 'a'; cin >> ch; cout << letter;

How are characters stored? The char data type sets aside one byte to store a character value. Recall that one byte equals 8 bits, where each bit contains either a zero or a one. What values do the 8 bits have when they represent various characters? A common encoding scheme for character data is ASCII, which stands for American Standard Code for Information Interchange.

Binary Numbers Any pattern of 0’s and 1’s represent a numeric value. Examples:

ASCII code In the ASCII code, bit 7 is always a zero. Since this leaves us with 7 bits 6 through 0 for our code, this means there are 128 possible patterns. (2 7 = 128.) An ASCII code chart will thus have 128 entries; because we start numbering from zero, the entries will be numbered 0 through 127.

ASCII Chart ASCII Code Chart

C-style Character Strings (C strings) Recall that the language C++ was based on the earlier language C. Unlike C++, the C language does not have the string data type. Instead, in C, strings of characters are stored in one-dimensional arrays of type char, one character per array element.

When we have placed a string of characters in double quotes, say in an output instruction, we have used C strings. For example: cout << "x="; Thus a list of characters contained in double quotes is a C-style character string, or C string. Because C strings are stored as an array, the C string "x=" is stored as

A C string is terminated by the special character '\0' called the NULL character. The NULL character acts as a sentinel and marks the end of a C-style character string. The string “x” and the character ‘x’ are stored differently as the pictures below demonstrate:

Character arrays are used to store C strings. For example: char lastName[10] = "Jackson"; will cause the array, lastName to contain:

If instead, we had initialized lastName with a string bigger than can be stored in the array, for example: char lastName[10] = "Washington"; then no error message will be generated in C++, but the following unfortunate situation will occur:

Input of C Strings char line[80]; char ch; int charCount = 0; // Read a line of text into the character // array "line". If the line is too long, // only read the first 79 characters cin.get(ch); while ( ch!='\n' && charCount<79 ) { line[charCount] = ch; charCount++; cin.get(ch); } // Place the NULL terminating character in the last position line[charCount] = '\0';

The operator >> can be used to read in a C string char name[20]; cout << "Please enter the name: " cin >> name; and our input is: Mouse, Mickey then internally, name will contain:

Output of C Strings the C string can be printed all at once using cout. Assume name is a character string initialized as follows: char name[15] = "Washington"; The output cout << endl << name; will cause the string "Washington" to be printed in the leftmost 10 positions of a new line.

Comparison of C Strings C++ provides an extensive collection of functions that allow the programmer to manipulate C strings. To use these functions, the string.h header file must be included in your program.

The function strcmp() has been provided to compare two C strings, say str1 and str2. If str1 < str2 (based on a character by character comparison), a value less than 0 is returned. If str1 and str2 are the same, a value of 0 is returned. If str1 > str2, a value greater than 0 is returned. The function makes character comparisons of the elements in str1 and str2 starting with the 0th character. It stops when it finds characters that are not equal or when it reaches the end of one of the strings.

Examples of String Comparisons: strcmp("A","B") returns < 0 (negative) strcmp("James","Jami") returns < 0 (negative) because 'e' < 'i' strcmp("135", "24") returns < 0 (negative) because '1' < '2' strcmp("ABCD","ABC") returns > 0 (positive) strcmp("ABC","ABCD") returns < 0 (negative) strcmp("89", "89") returns 0 (zero)

Copying (assigning) a C String C string assignment (or string copy) is achieved by the function strcpy(). This function has two arguments, a destination string and a source string. No check is made to determine whether or not the destination string has enough space! All characters from thesource up to and including the '\0' are copied.

Example of Copying a string: Example: char name[15]; strcpy(name, "Mr. Mouse"); will cause the variable name to contain:

C String Length C++ provides a function,called strlen(), that returns the number of characters in a C string (up to but not including the NULL character). For example, strlen("Mr. Mouse") returns the value 9.