String in C++. Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

 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.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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 Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
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.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
CS Nov 2006 C-strings.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
Introduction to C programming
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
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.
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Characters, Strings, And The string Class Chapter 10.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
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.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CSC 270 – Survey of Programming Languages
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
 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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Strings, Slide Fundamental Programming Strings.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
LESSON 2 Basic of C++.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
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.
Characters, Strings, and the cstring Library
LESSON 2 Basic of C++.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Characters, Strings, and the cstring Library
Strings A string is a sequence of characters treated as a group
CS111 Computer Programming
INC 161 , CPE 100 Computer Programming
מחרוזות-String בשפת C++ ישנו תפקיד מיוחד למערך מסוג char רצף של תווים הנמצאים במערך מסוג char המסתיימת בתו אפס (הכוונה לאפס ממש '0\' , ולא לתו '0')
String in C++.
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Strings #include <stdio.h>
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Programming Strings.
Library in c++ ( as cmath , iostream , …… etc.)
String in C++.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

String in C++

Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; str3 = str1; // copy str1 into str3 cout << "str3 : " << str3 << endl; // concatenates str1 and str2 str3 = str1 + str2; cout << "str1 + str2 : " << str3 <<endl; //total length of str3 after concatenation len = str3.size(); cout << "str3.size() : " << len <<endl; }

Example using build in functions of class string #include using namespace std; void main() { string first, last, title; string wholeName; string greeting= "Hello "; cout<<"Enter first name: "; cin>>first; cout<<"Enter last name: "; cin>>last; wholeName=first+" "+last; cout<<greeting<<wholeName<<'!'<<endl; cout<<"You have "<<(wholeName.length()-1)<<" letters in your name. "<<endl;

cout<<"Your initials are"<< first.at(0)<< last.at(0); cout<<"\n position of last "<<wholeName.find(last) ; cout<<endl; wholeName.insert(0,"Dr."); cout<<greeting<<wholeName<<'!'<<endl; wholeName.insert(9,"sami "); cout<<greeting<<wholeName<<'!'<<endl; wholeName.replace(9,7,"F."); cout<<greeting<<wholeName<<'!'<<endl; wholeName.erase(9,3); cout<<greeting<<wholeName<<'!'<<endl; title.assign(wholeName,0,4); cout<<greeting<<wholeName<<'!'<<endl; cout<<greeting<<title<<'!'<<endl; }

The C-Style Character 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’} or char sname[10 ] = {‘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 Copies string s2 into string s1. #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 Concatenates string s2 onto the end of string s1. #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 is a function that accepts a string, defined as an array of characters, and returns the number of characters in the string excluding null character #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"; }