String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
 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.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
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.
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.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
String in C++. Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏
February 14, 2005 Characters, Strings and the String Class.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
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.
Characters, Strings, And The string Class Chapter 10.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Data Types Declarations Expressions Data storage C++ Basics.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
C++ Programming Basics
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.
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)
Chapter Characters, Strings, and the string class 10.
ENEE150 – 0102 ANDREW GOFFIN Strings. Project 2 Flight Database 4 options:  Print flight  Print airport  Find non-stop flights  Find one-stop flights.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
LESSON 2 Basic of C++.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
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.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Characters and Strings
Pointers, Dynamic Data, and Reference Types
String in C++.
Introduction to Programming
C++ Pointers and Strings
C Programming Lecture-8 Pointers and Memory Management
Strings Skill Area 313 Part C
Characters and Strings
C++ Pointers and Strings
String in C++.
Presentation transcript:

String in C++

String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character ‘\0’. char sname[ ] = “Ali Samer”; // 10 locs or char sname[ ] = {‘A’,’l’,’i’,’ ’,‘S’,’a’,’m’,’e’,’r’,’\0’}

String String can be constant pointer that points to the string’s first character. char *sname = “Ali Samer”; Sname points to ‘A’ “Ali Samer’\0’” in Some where in memory

Example void main( ) { char firstName[] = "Ahmad"; char *lastName = "Omar"; cout<<"First Name: "<<firstName<<endl; cout<<"Last Name: "<<lastName<<endl; int i=0; cout<<"FirstName: "; while (firstName[i] != '\0') cout<<firstName[i++]; i=0; cout<<"\nLast Name: "; while (lastName[i] != '\0') cout<<lastName[i++]; }

Reading String char studentName[20]; cin>>studentName; cout<<studentName<<endl; Problem: read characters until the first white space.

Reading String solution: use cin.getline(array_name, array_size, delimiter) char studentName[20]; cin.getline(studentName,20,'\n'); cout<<studentName<<endl; Remark: Pointer to character string can not be readed

String Library cstring.h or string.h This library include set of built in functions for manipulating the strings. Like: 1- strcpy(s1, s2)  s1 = s2 #include void main() { char str1[] ="Omar"; char *str2 = "Ahmad"; strcpy(str1,str2); cout<<str1<<endl; }

2- strncpy(s1, s2)  s1[n] = s2[n] #include void main() { char str1[] = ” **********"; char *str2 = “ $$$$$$$$$$"; strncpy(str1,str2,5); cout<<str1<<endl; }

3- strcat(s1, s2)  s1 = s1+s2 #include void main() { char str1[24] = ” Philadelphia"; char *str2 = “ University"; strcat(str1,str2); cout<<str1<<endl; }

4- strncat(s1, s2,n)  s1 = s1+s2[n] #include void main() { char str1[24] = ” Philadelphia"; char *str2 = “ University of Jordan"; strncat(str1,str2,10); cout<<str1<<endl; }

5- strcmp(s1, s2)  0 if s1 = s2  -1 if s1 < s2  1 if s1 > s2 Symbols < … < numbers < … < capital letters < …. < smal letters. #include void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strcmp(str1,str2)) if (strcmp(str1,str2) == 1) cout "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; else cout<<str1<<" = "<<str2<<endl; }

6- strncmp(s1, s2,n)  0 if s1[n] = s2[n]  -1 if s1[n] < s2[n]  1 if s1[n] > s2[n] #include void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strncmp(str1,str2,1)) if (strcmp(str1,str2,1) == 1) cout "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; else cout<<str1<<" = "<<str2<<endl; }

7- strlen(s)  How many characters in s #include void main() { char s1[] = "Ahamd Ali"; char *s2 = "Amman City"; cout<<s1<<" Consists of "<<strlen(s1)<<" Characters.\n"; cout<<s2<<" Consists of "<<strlen(s2)<<" Characters.\n"; }