Strings A collection of characters taken as a set:

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
AU/MITM/1.6 By Mohammed A. Saleh 1. Introducing the string Class  Instead of using a character array to hold a string, you can use a type string variable.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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:
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
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.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
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.
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 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.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Chapter 15 Strings as Character Arrays
Sahar Mosleh California State University San MarcosPage 1 Character String.
Strings, Slide Fundamental Programming Strings.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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,
Pointers and Dynamic Arrays
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Computer Programming BCT 1113
Arrays Arrays exist in almost every computer language.
Lecture-5 Arrays.
Characters, C-Strings, and More About the string Class
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
C++ Arrays.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
CS111 Computer Programming
C Stuff CS 2308.
Popping Items Off a Stack Lesson xx
Week 9 – Lesson 1 Arrays – Character Strings
Arrays November 8, 2017.
Review for Final Exam.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Chapter 9 One-Dimensional Arrays
10.1 Character Testing.
String in C++.
String What it is Why it’s useful
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Review for Final Exam.
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
String manipulation string.h library
Arrays Arrays A few types Structures of related data items
Exercise Arrays.
C++ Programming Lecture 20 Strings
Using string type variables
(Dreaded) Quiz 2 Next Monday.
Strings #include <stdio.h>
Strings …again.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Introduction to Problem Solving and Programming
Presentation transcript:

Strings A collection of characters taken as a set: A word, a phrase, a name, an address, a phone number, a Social Security Number In a program, Strings are stored as a unit in a char array. A special character is added to the end of the characters to mark the end of the String, an ASCII 0 character. The arrays need to be declared one element larger than the largest String you expect. ASCII 0 Character - the char whose ASCII value is 0. SHOW THEM THE LAYOUT OF A STRING IN AN ARRAY.

cout << "Enter your first name: "; cin >> firstName; char firstName[16], lastName[26]; cout << "Enter your first name: "; cin >> firstName; cout << "Enter your last name: "; cin >> lastName; cout << "Your name is " << firstName << " " << lastName << ".\n"; Enter your first name: Lee Enter your last name: Weiner Your name is Lee Weiner. Press any key to continue Notice I just use the name of the arrays in the cin and cout statements. No empty brackets. NEXT to show output.

Assigning Strings to Array Variables Initializing the array when declared char firstName[16] = "Lee", lastName[26] = "Weiner"; Using cin for user input cin >> firstName; In an assignment statement firstName = "Susan"; EACH BULLET APPEARS SEPARATELY

Some Other Things I Can't Do With Strings cout << firstName + LastName; if( firstName == lastName ) if( firstName < secondName ) firstName = secondName;

String Functions strlen( <string> ) - Returns an int that is the number of characters in the string. strcpy( <target>, <source> ) - Copies the contents of the source string to the target variable. Also used in place of an assignment statement. strcmp( <str1>, <str2> ) - Compares two strings. Returns an int indicating the relationship between the two. Need to #include <string.h>