String in C++.

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.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
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.
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
String in C++. Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
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.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 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.
LESSON 2 Basic of C++.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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,
Lecture 2A Data Types Richard Gesick
Strings: C-strings vs. Strings as Objects
Characters, Strings, and the cstring Library
LESSON 2 Basic of C++.
INC 161 , CPE 100 Computer Programming
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
Chapter 8 - Characters and Strings
CS111 Computer Programming
Object Oriented Programming COP3330 / CGS5409
Remark: Data Type of Array Name
Strings: C-strings vs. Strings as Objects
Pointers and Pointer-Based Strings
String What it is Why it’s useful
EECE.2160 ECE Application Programming
Engineering Problem Solving with C++, Etter
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
Using string type variables
Lecture 2 Arrays & Pointers September 7, 2004
Library in c++ ( as cmath , iostream , …… etc.)
Characters and Strings
EECE.2160 ECE Application Programming
String in C++.
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

String in C++

Using Strings in C++ Programs String library <string> or <cstring> provides functions to: - manipulate strings - compare strings - search strings ASCII character code - Strings are compared using their character codes - Easy to make comparisons (greater than, less than, equal to)

Using Strings in C++ Programs .. Cont. Character Constant - Integer value of a character - Represented with single quotes - ‘z’ is the integer value of z, which is 122 String in C++ - Series of characters treated as one unit - can include letters, digits, special characters +, -, *, … - String literal (string constants) enclosed in double quotes, for example: “C++ course”

Using Strings in C++ Programs .. Cont. Example: Write a C++ program that reads two initials and the last name of a person and displays a personalized message to the program user. Analysis stage: Input: 2 characters for the initials (e.g. first and second) 1 string for the last name (e.g. last) Output: a message to welcome the user

Using Strings in C++ Programs .. Cont. //A program to display a user’s name with a welcome message #include <iostream> #include <string> using namespace std; void main ( ) { char first, second; //input and output: first and second initials string last; //input and output: last name // Enter letters and print message. cout<<"Enter 2 initials for your first and second names and last name: " ; cin >> first >> second >> last; cout<< "Hello "<<first<< ". " << second<<". " <<last<< endl; }

Using build in library. #include <iostream> #include <string> 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; len = str3.size(); cout << "str3.size() : " << len <<endl; }

Using built-in libraray – Cont. #include<iostream> #include<string> 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)<<endl; cout<<"position of last "<<wholeName.find(last)<<endl; wholeName.insert(0,"Mr. "); cout<<greeting<<wholeName<<'!'<<endl; wholeName.insert(9,"Ali "); wholeName.replace(9,7,"Maher "); wholeName.erase(9,3); title.assign(wholeName,0,20); cout<<greeting<<title<<'!'<<endl; }

Output :

Fundamentals of Strings in C++ String can be array of characters ends with null character ‘\0’. char color [ ] = “green” ; -- this creates 6 element char array, color, (last element is ‘\0’) -- color can be declared also as : char color [ ] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘\0’ }; char color [ 6] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘\0’ }; g r e n \0

Fundamentals of Strings in C++ .. Cont. - String can be constant pointer that points to the string’s first character. Example: char *colorPtr = “green” ; -- this creates pointer variable colorPtr that points to the string “green” that is stored somewhere in memory -- value of variable colorPtr is the address of its first character(g) g r e n \0

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++]; }

Fundamentals of Strings in C++ .. Cont. Reading Strings - Assign input to character array, for example char word [ 20 ]; cin >> word; cout<<word<<endl; -- this reads characters until a space, tab, newline, or end-of-file is encountered. -- the string should be less than 19 characters, the 20th is for the null character (‘\0’). Problem: read characters until the first white space

Fundamentals of Strings in C++ .. Cont. solution: To read an entire line of text into an array, C++ uses: getline function as follows: cin.getline ( array, array size, delimiter character); - getline will copy input into specified array until either -- one less than the size is reached -- the delimiter character is input - Example: char word [20] ; cin.getline ( word, 20, ‘\n’ );

String Manipulation Functions Description Function Copies string s2 into the character array s1. The value of s1 is returned. char *strcpy(char *s1, const char *s2); Copies at most n characters of string s2 into the array s1. The value of s1 is returned. char *strncpy(char *s1, const char *s2, size_t n); Appends string s2 to string s1. The value of s1 is returned. char *strcat (char *s1, const char *s2); Appends at most n characters of string s2 to string s1. The value of s1 is returned. char *strncat (char *s1, const char *s2, size_t n);

String Manipulation Functions .. Cont. Compares string s1 with string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. int strcmp(const char *s1, const char *s2); Compares up to n characters of string s1 with string s2. The function returns zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. int strncmp(const char *s1, const char *s2, size_t n);

String Manipulation Functions .. Cont. Determines the length of string s. The number of characters preceding the terminating null character is returned. Size_t strlen( const char *s); Note: These functions only work with Array of characters and pointer. They don’t work on variables of type string.

String Manipulation Functions .. Cont. 1- strcpy(s1, s2)  s1 = s2 Copies string s2 into string s1. #include <iostream> #include <cstring> using namespace std; void main() { char str1[] ="Omar"; char *str2 = "Ahmad"; strcpy(str1,str2); cout<<str1<<endl; }

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

Concatenates string s2 onto the end of string s1. 3- strcat(s1, s2)  s1 = s1+s2 Concatenates string s2 onto the end of string s1. #include <iostream> #include <cstring> using namespace std; 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 <iostream> #include <cstring> using namespace std; 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 <iostream> #include <cstring> using namespace std; 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<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; cout<<str1<<" = "<<str2<<endl; }

6- strncmp(s1, s2,n)  0 if s1[n] = s2[n] #include <iostream> #include <cstring> using namespace std; 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<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; 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 <iostream> #include <cstring> using namespace std; 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"; }

strcpy - code #include<iostream> using namespace std; const int stringsize = 40; void strcpy(char destation[], char source[]) { int stringindex=0; while(source[stringindex] !='\0') { destation[stringindex]=source[stringindex]; stringindex++;} destation[stringindex]='\0'; } void main() { char s1[stringsize]; // or you can define it char *s1 = new char[]; strcpy(s1,"we are going to implement strings"); cout<<"S1 = "<< s1<<endl;

strlen- code #include<iostream> using namespace std; const int stringsize =256; int strlen(char string[]) { int len=0; while(string[len]!='\0') len++; return len;} void main() { char buffer[stringsize]; char buffer2[] = "this is another expression"; cout<<"enter an expression"<<endl; cin>>buffer; cout<<"the length of the string you just typed is " <<strlen(buffer)<<endl; cout<<"the length of the string with space is " <<strlen(buffer2)<<endl;}

strcmp - code #include<iostream> using namespace std; const int stringsize = 256; int strcmp(char s1[], char s2[]) { int stringindex=0; while(s1[stringindex] !='\0') { if (s1[stringindex]>s2[stringindex]) return 1; else if (s1[stringindex]<s2[stringindex]) return -1; else stringindex++;} if (strlen(s1) < strlen(s2)) return 0;} void main() { char b1[]="Ahmad"; char b2[]="Ali"; if(strcmp(b1,b2) == -1) cout<<"first string is smaller"<<endl; else if (strcmp(b1,b2) == 1) cout<<"first string is larger"<<endl; else cout <<"both strings are the same"<<endl;}

strcat - code void main() { char s1[265]; #include<iostream> using namespace std; void strcat(char destination[], char source[]) { int len=0; int stringindex=0; // calculates the end of the destination string while(destination[len]!='\0') len++; while(source[stringindex] != '\0') { destination[len+stringindex]=source[stringindex]; stringindex++; } destination[len+stringindex]='\0'; void main() { char s1[265]; strcpy(s1,"philadelphia University "); strcat(s1,"- Faculty of Information Technology"); cout<<" the string is: "<<endl<<s1<<endl; }