Library in c++ ( as cmath , iostream , …… etc.)

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.
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.
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
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
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.
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.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
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.
Sahar Mosleh California State University San MarcosPage 1 Character String.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Arrays, Character Strings and Standard Type String ~ Collection.
Strings, Slide Fundamental Programming Strings.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
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.
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.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
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
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
LESSON 2 Basic of C++.
INC 161 , CPE 100 Computer Programming
CMSC 202 Lesson 2 C++ Primer.
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Quiz 11/15/16 – C functions, arrays and strings
CS111 Computer Programming
Object Oriented Programming COP3330 / CGS5409
CSC1201: Programming Language 2
C Stuff CS 2308.
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Strings: C-strings vs. Strings as Objects
מחרוזות-String בשפת C++ ישנו תפקיד מיוחד למערך מסוג char רצף של תווים הנמצאים במערך מסוג char המסתיימת בתו אפס (הכוונה לאפס ממש '0\' , ולא לתו '0')
Strings A collection of characters taken as a set:
String class and its objects
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String in C++.
String What it is Why it’s useful
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++, Etter
CSC1201: Programming Language 2
C++ Programming Lecture 20 Strings
Strings in C Array of characters is called a string.
Using string type variables
Strings #include <stdio.h>
Today’s Objectives 28-Jun-2006 Announcements
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Programming Strings.
CS31 Discussion 1H Fall18: week 6
An Introduction to STL.
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.
cout << str1;  pear
Dr. Khizar Hayat Associate Prof. of Computer Science
Introduction to Problem Solving and Programming
Presentation transcript:

Library in c++ ( as cmath , iostream , …… etc.) 1 Array of characters 2 Can contain small,capital letters , numbers and symbols 3 summary Terminated by null character ( ‘/0’ ) 4 #include <string> 5 Can be treated as integers 6

String is a sequence of characters ending with NULL '\0' char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; If you follow the rule of array initialization, then you can write the above statement as follows: char greeting[] = "Hello"; Following is the memory presentation of above defined string in C++: Actually, you do not place the null character at the end of a string constant. The C++ compiler automatically places the '\0' at the end of the string when it initializes the array.

String declaration: before declaring a string we need to call the string header library as following: #include<string> char a[4]="abc"; // == char a[4]={'a','b','c','\0'}; String x (“highschool”) ; String x = “highschool” ; String x ; x = “highschool” ;

Operations on strings Working Operator Assignment = Joining two or more strings + Concatenation and assignment += Equality == Not equal to != Less than , greater than < , > Less than or equal <= Greater than or equal >=

String Input: We can use the (cin) to input string as any other data types: cin>>s; cin>>s[i] ; Example: #include<iostream> #include<string> Main(){ char word[80] ; Do{ cin >>word ; Cout << "\t“ << word << endl ; } while (word!=0)}

Example: a program to read a string and print its elements: #include <iostream> #include <string> int main() { char s[]= "ABCDEF” ; for (int i=0;i<7;i++) cout<<"s["<<i<<"]=” <<s[i]<<endl; } Output : s[0]='A' s[1]='B' s[2]='C' and so on.

String functions Most common : 1- length 2- Copy 3- Concatenation 4- Compare 5- Assignment

Assign())) Assignment #include <iostream> #include <string.h> using namespace std ; int main () { string s1 ("c plus plus "); string s2,s3,s4 ; s2.assign (s1) ; s3.assign (s1 , 0 , 6) ; s4.assign (s1 , 4 , 2) ; cout << s2<<endl<<s3<<endl<<s4 ; return 0; } Out put : c plus plus c plus us

Length function ( strlen ) strlen(str) used to find the length of string, i.e., the number of characters excluding the null terminator. Ex.: strlen("ABC") output : 3 (integer number). Ex.: int length; char a[]="XYZW"; length=strlen(a); output: 4.

#include <iostream> #include <string> Using namespace std ; int main () { //length () : determinates the length i.e. no. of characters string s1 ; cout << “length : “ << s1.length () <<endl ; s1 = “ hello “ ; cout << “length : “ << s1.length () << endl ; return 0 ; } Output : Length : 0 Length : 5

Example: write a c++ program to read a string and count the string length without using strlen() function: Solution: #include<iostream.h> #include<string.h> void main(){ char s[256]; int i,k; cout<<"insert a string"<<endl; cin>>s; k=0; for(i=0;s[i]!='\0';i++) k++; cout<<"length of the string is:"<<k<<endl; }

Strcmp ( compare function) Comparison of Strings The strcmp(str_1, str_2) function compares two strings and returns the following result : str_1 == str_2 : 0 str_1 > str_2 : positive number str_1 < str_2 : negative number The strings are compared lexicographically (i.e., according to dictionary order): a < aa < aaa < … < b < ba < bb < … < bz < baa < … < abca < abd < ...

#include <iostream> #include <string #include <iostream> #include <string.h> using namespace std ; int main () { string s1 ("abd") ; string s2 ( "abc") ; int d = s1 . compare (s2) ; if ( d == 0 ) cout << " \n Both strings are identical " ; else if ( d > 0 ) cout << "\n s1 is greater than s2 " ; else cout << "\n s2 is greater than s1 " ; return 0 ; }

Swap function (strswp) Swap string exchange the given string with another string . #include <iostream> #include <string.h> using namespace std ; int main () { string buyer ("money"); string seller ("goods"); cout << "Before the swap, buyer has " << buyer; cout << " and seller has " << seller << '\n'; swap (buyer,seller); cout << " After the swap, buyer has " << buyer; return 0; } Out put : Before the swap, buyer has money and seller has goods After the swap, buyer has goods and seller has money

#include <string> #include <iostream> int main( ) { using namespace std ; // Declaring an object of type basic_string<char> string s1 ( "Tweedledee" ); string s2 ( "Tweedledum" ) ; cout << "Before swapping string s1 and s2 : " << endl; cout << "The basic_string s1 = " << s1 << "." << endl; cout << "The basic_string s2 = " << s2 << "." << endl; swap ( s1 , s2 ); cout << "\n After swapping string s1 and s2:" << endl; }

Copy function ( strcpy) strcpy(s1,s2) : copy s2 instead of s1. Example: char s1[]="abc"; char s2[]="xyz"; strcpy(s1,s2); cout<<s1<<endl; cout<<s2<<endl; output: xyz xyz

strncpy(s1,s2,n): copy the first n characters of s2 into s1 strncpy(s1,s2,n): copy the first n characters of s2 into s1. Example: char s1[]="abcde"; Char s2=[]="xyz"; Strncpy(s1,s2,2); cout<<s1<<endl; cout<<s2<<endl; output: xycde xyz

Cocatenation function (strcat(),strncat()) strcat(s1,s2): insert s2 in the end of s1. Strlen(s1) will be strlen(s1)+strlen(s2). Example: char s1[]="abc"; Char s2=[]="xyz"; Strcat(s1,s2); cout<<s1<<endl; cout<<s2<<endl; output: abcxyz xyz

strncat(s1,s2,n): insert n characters from s2 in the end of s1 Example: char s1[]="abc"; Char s2=[]="xyz"; Strncat(s1,s2,2); cout<<s1<<endl; cout<<s2<<endl; output: abcxy xyz

#include <iostream> #include <string> using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; str3 = str1; // copy str1 into str3 cout << "str3 : " << str3 << endl; str3 = str1 + str2; // concatenates str1 and str2 cout << "str1 + str2 : " << str3 << endl; len = str3.size(); // total length of str3 after concatenation cout << "str3.size() : " << len << endl; return 0; } Out put : str3 : Hello str1 + str2 : HelloWorld str3.size() : 10

#include <iostream> #include <string> using namespace std; int main () { char str1[10] = "Hello"; char str2[10] = "World"; char str3[10]; int len ; strcpy( str3, str1); // copy str1 into str3 cout << "strcpy( str3, str1) : " << str3 << endl; strcat( str1, str2); // concatenates str1 and str2 cout << "strcat( str1, str2): " << str1 << endl; len = strlen(str1); // total length of str1 after concatenation cout << "strlen(str1) : " << len << endl; return 0; } Out put: strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10

Example : write a C++ program to enter a matrix of 2-D string names and search for a name entered by keyboard to end the program when find it: Solution: #include<iostream> #include<string.h> void main(){ int i=0, t=0; char myname [10]; char name[5][10]={"ali","huda","hadi","deyaa","samy"}; do{ cout<<"enter your name"; cin>>myname; for (i=0;i<5;i++) { if(strcmp(myname, name[i])==0) t=1; } while(t==0) cout << " Welome " ; }

What is the out put ? Out put : 10 #include <iostream> #include <cstring> using namespace std; int main () { char str1[10] = "Hello"; char str2[10] = "World"; char str3[10]; int len ; strcpy( str3, str1); strcat( str1, str2) len = strlen(str1); cout << len << endl; return 0; } a) 5 b) 55 c) 11 d) 10 Out put : 10

What is the out put ? #include <iostream> #include <string> using namespace std; int main () { string str ("Ubuntu"); cout << str.capacity(); cout << str.max_size(); return 0; } a) 61073741820 b) 51073741820 c) 6 and max size depends on compiler d) none of the mentioned Answer : c