watch a video here for beginneers
in c++ programming language
https://www.youtube.com/channel/UCThawdb6qPuk3wkCtinhLDg
1. A string is a one-dimensional array of characters terminated by a null character. Strings can be initialized during compilation or at runtime.
2. Common string functions like scanf(), gets(), getchar() are used to input strings while printf(), puts(), putchar() are used to output strings.
3. Library functions like strcpy(), strcat(), strcmp(), strlen() allow manipulation of strings like copying, concatenation, comparison and finding length.
This document discusses string handling in C++. It defines a string as a collection of characters written in double quotation marks. Strings can be declared and initialized similarly to character arrays. The cin object and cin.getline() function can be used to input strings with or without spaces. Arrays of strings are two-dimensional character arrays that store multiple strings. Common string functions include memcpy() to copy characters, strcmp() to compare strings, strcpy() to copy one string to another, strlen() to find the length of a string, and strcat() to concatenate two strings.
The document discusses strings in C++. It defines strings, describes how to declare and manipulate string objects using the string class in C++, and lists common string operations like concatenation, comparison, extracting substrings, searching/finding patterns, and inserting/replacing/deleting substrings. It also discusses how to work with C-style strings using functions in the <strings.h> library and character functions in <ctype.h>.
Strings are traditionally sequences of characters that can be represented as variables or constants in programming. A string is stored as an array of characters with a null terminator. Common string operations include comparison, concatenation, copying and getting the length. Strings can be passed to functions as character arrays and there are safe string functions that allow operating on strings up to a specified number of characters to avoid buffer overflows.
Strings are a fundamental data type that represent sequences of characters. In C++, a string is stored as an array of characters with a null terminator. Common string operations include length, concatenation, comparison, and copying. Strings can be passed to functions by passing the array name and manipulated using library functions like strlen(), strcat(), strcpy(), and strcmp(). Arrays of strings allow storing multiple string values.
Strings are traditionally sequences of characters that can be represented as variables or constants in programming. A string is stored as an array of characters with a null terminator. Common string operations include comparison, concatenation, copying and getting the length. Strings can be passed to functions as character arrays and there are safe string functions that allow operating on strings up to a specified number of characters to avoid buffer overflows.
The document discusses various methods for working with C++ strings, including:
1. Reading strings from the keyboard using getline() and specifying the string size and optional terminating character.
2. Comparing strings using operators like ==, !=, <, <=, >, >=.
3. Accessing individual characters of a string using the index operator [].
4. Obtaining the length of a string using length() or size(), checking if a string is empty, and extracting substrings.
The document discusses strings in C programming. It defines strings as sequences of characters stored as character arrays that are terminated with a null character. It covers string literals, declaring and initializing string variables, reading and writing strings, and common string manipulation functions like strlen(), strcpy(), strcmp(), and strcat(). These functions allow operations on strings like getting the length, copying strings, comparing strings, and concatenating strings.
STRING BASICS
DECLARING AND INITIALIZING
POINTERS FOR STRING MANIPULATION
STRING HANDLING FUNCTIONS: https://onlinegdb.com/zaPP6-oMS
CHARACTER ORIENTED FUNCTIONS : https://onlinegdb.com/pp7DrUBRlc
TWO DIMENSIONAL ARRAY OF STRINGS
This document discusses strings in C programming. It defines strings as null-terminated 1D character arrays. Common string operations like reading, comparing, copying and concatenating strings can be done using library functions like scanf(), printf(), strlen(), strcat(), strcmp(), and strcpy() rather than reimplementing them. These functions make string handling easier in C. The document provides examples and explanations of how to use these standard string library functions.
This document discusses strings in C programming. It defines strings as null-terminated 1D character arrays. The null character '\0' is used to mark the end of a string. String constants are enclosed in double quotes and include a null terminator. Functions like scanf(), printf(), strlen(), strcat(), strcmp(), and strcpy() from the string.h library can be used to work with strings. These functions allow operations like reading, printing, finding the length of, concatenating, comparing, and copying strings.
The document discusses strings in C and common string functions. It defines a string as an array of characters terminated by a null character. It describes two ways to use strings - with a character array or string pointer. It then explains functions such as strcpy(), strcat(), strcmp() that copy, append, or compare strings. Other functions like memcpy(), memcmp() operate on a specified number of characters rather than null-terminated strings.
The document discusses strings in C programming. It defines strings as finite sequences of characters that can be implemented as arrays of bytes or characters. It describes common string operations like searching, sorting, trimming, insertion, and deletion. It also lists various string manipulation functions in C like strcat, strcpy, strlen, and their applications in areas like text editing, search engines, and computational biology.
This document discusses handling character strings in C. It covers:
1. How strings are stored in memory as ASCII codes appended with a null terminator.
2. Common string operations like reading, comparing, concatenating and copying strings.
3. How to initialize, declare, read and write strings.
4. Useful string handling functions like strlen(), strcpy(), strcat(), strcmp() etc to perform various operations on strings.
The document discusses strings in C++. It defines strings as arrays of characters and describes how to declare, initialize, input, and output strings. It also discusses storing multiple strings using a 2D character array. Finally, it lists and provides the syntax for various string handling functions in C++ like strcpy(), strcat(), strlen(), strcmp(), etc.
This document provides an overview of string handling in C programming. It discusses how strings are represented as character arrays and terminated with a null character. It describes declaring, initializing, and manipulating strings through built-in string functions like strlen(), strcpy(), strcmp(), strcat(), strlwr(), and strrev(). Examples are given to illustrate how each string function works and how to use them to process strings as complete entities.
The document discusses strings in C including how to declare, initialize, input, output, and manipulate strings using standard library functions as well as how to manage arrays of strings. It provides examples of declaring and initializing strings, using scanf and gets to input strings, common string manipulation functions like strlen and strcpy, and demonstrates how to work with arrays of strings such as storing and sorting a list of names.
The document is a slide presentation on strings in C++. It covers topics like the concept of strings as character arrays, string manipulation functions from the string handling library like strcpy(), strcat(), strcmp(), and examples of using these functions. It also discusses fundamentals of characters and strings, comparing strings, and common errors with strings. The presentation is delivered by Dr. Ali Mirza of the Computer Science department at Bahria University for their CSC 113 computer programming course.
This document provides information about strings in C++. It defines a string as a collection of characters within double quotes. Strings are stored as character arrays terminated by a null character. The document discusses declaring and initializing strings, inputting strings from the user, and functions for manipulating strings like strcat(), strcmp(), and memcpy(). It also covers arrays of strings and comparing, concatenating, copying and other operations on strings.
1. Strings in C are 1-dimensional arrays of type char that are terminated with a null character '\0'.
2. Character arrays can represent strings when terminated with a null character. Common string functions like strlen, strcat, strcmp, and strcpy in the string.h library allow manipulating strings.
3. strlen returns the length of a string by counting the characters until the null terminator. strcat concatenates two strings by appending the second string to the first. strcmp compares two strings lexicographically and returns less than 0 if the first is less than the second.
This document discusses strings in C programming. It defines strings as arrays of characters that end with a null terminator (\0). It explains how to initialize and print strings. Common string functions like strlen(), strcpy(), strcat(), and strcmp() are described. The document contrasts strings and character pointers, noting strings cannot be reassigned while pointers can. Finally, it lists and briefly explains other standard string library functions.
This document contains an interview with Colin Dean, a computer science graduate. In the interview, Dean discusses his realization that he wanted to study computer science, provides advice for first-year students, and shares details about his education and career. He also answers questions about whom he would like to have dinner with, what technology blogs he reads, and where he sees himself in ten years. The document concludes with biographical information about Colin Dean.
watch a video here for beginneers
in c++ programming language
https://www.youtube.com/channel/UCThawdb6qPuk3wkCtinhLDg
More Related Content
Similar to Chapter 4 (Part II) - Array and Strings.pdf (20)
The document discusses various methods for working with C++ strings, including:
1. Reading strings from the keyboard using getline() and specifying the string size and optional terminating character.
2. Comparing strings using operators like ==, !=, <, <=, >, >=.
3. Accessing individual characters of a string using the index operator [].
4. Obtaining the length of a string using length() or size(), checking if a string is empty, and extracting substrings.
The document discusses strings in C programming. It defines strings as sequences of characters stored as character arrays that are terminated with a null character. It covers string literals, declaring and initializing string variables, reading and writing strings, and common string manipulation functions like strlen(), strcpy(), strcmp(), and strcat(). These functions allow operations on strings like getting the length, copying strings, comparing strings, and concatenating strings.
STRING BASICS
DECLARING AND INITIALIZING
POINTERS FOR STRING MANIPULATION
STRING HANDLING FUNCTIONS: https://onlinegdb.com/zaPP6-oMS
CHARACTER ORIENTED FUNCTIONS : https://onlinegdb.com/pp7DrUBRlc
TWO DIMENSIONAL ARRAY OF STRINGS
This document discusses strings in C programming. It defines strings as null-terminated 1D character arrays. Common string operations like reading, comparing, copying and concatenating strings can be done using library functions like scanf(), printf(), strlen(), strcat(), strcmp(), and strcpy() rather than reimplementing them. These functions make string handling easier in C. The document provides examples and explanations of how to use these standard string library functions.
This document discusses strings in C programming. It defines strings as null-terminated 1D character arrays. The null character '\0' is used to mark the end of a string. String constants are enclosed in double quotes and include a null terminator. Functions like scanf(), printf(), strlen(), strcat(), strcmp(), and strcpy() from the string.h library can be used to work with strings. These functions allow operations like reading, printing, finding the length of, concatenating, comparing, and copying strings.
The document discusses strings in C and common string functions. It defines a string as an array of characters terminated by a null character. It describes two ways to use strings - with a character array or string pointer. It then explains functions such as strcpy(), strcat(), strcmp() that copy, append, or compare strings. Other functions like memcpy(), memcmp() operate on a specified number of characters rather than null-terminated strings.
The document discusses strings in C programming. It defines strings as finite sequences of characters that can be implemented as arrays of bytes or characters. It describes common string operations like searching, sorting, trimming, insertion, and deletion. It also lists various string manipulation functions in C like strcat, strcpy, strlen, and their applications in areas like text editing, search engines, and computational biology.
This document discusses handling character strings in C. It covers:
1. How strings are stored in memory as ASCII codes appended with a null terminator.
2. Common string operations like reading, comparing, concatenating and copying strings.
3. How to initialize, declare, read and write strings.
4. Useful string handling functions like strlen(), strcpy(), strcat(), strcmp() etc to perform various operations on strings.
The document discusses strings in C++. It defines strings as arrays of characters and describes how to declare, initialize, input, and output strings. It also discusses storing multiple strings using a 2D character array. Finally, it lists and provides the syntax for various string handling functions in C++ like strcpy(), strcat(), strlen(), strcmp(), etc.
This document provides an overview of string handling in C programming. It discusses how strings are represented as character arrays and terminated with a null character. It describes declaring, initializing, and manipulating strings through built-in string functions like strlen(), strcpy(), strcmp(), strcat(), strlwr(), and strrev(). Examples are given to illustrate how each string function works and how to use them to process strings as complete entities.
The document discusses strings in C including how to declare, initialize, input, output, and manipulate strings using standard library functions as well as how to manage arrays of strings. It provides examples of declaring and initializing strings, using scanf and gets to input strings, common string manipulation functions like strlen and strcpy, and demonstrates how to work with arrays of strings such as storing and sorting a list of names.
The document is a slide presentation on strings in C++. It covers topics like the concept of strings as character arrays, string manipulation functions from the string handling library like strcpy(), strcat(), strcmp(), and examples of using these functions. It also discusses fundamentals of characters and strings, comparing strings, and common errors with strings. The presentation is delivered by Dr. Ali Mirza of the Computer Science department at Bahria University for their CSC 113 computer programming course.
This document provides information about strings in C++. It defines a string as a collection of characters within double quotes. Strings are stored as character arrays terminated by a null character. The document discusses declaring and initializing strings, inputting strings from the user, and functions for manipulating strings like strcat(), strcmp(), and memcpy(). It also covers arrays of strings and comparing, concatenating, copying and other operations on strings.
1. Strings in C are 1-dimensional arrays of type char that are terminated with a null character '\0'.
2. Character arrays can represent strings when terminated with a null character. Common string functions like strlen, strcat, strcmp, and strcpy in the string.h library allow manipulating strings.
3. strlen returns the length of a string by counting the characters until the null terminator. strcat concatenates two strings by appending the second string to the first. strcmp compares two strings lexicographically and returns less than 0 if the first is less than the second.
This document discusses strings in C programming. It defines strings as arrays of characters that end with a null terminator (\0). It explains how to initialize and print strings. Common string functions like strlen(), strcpy(), strcat(), and strcmp() are described. The document contrasts strings and character pointers, noting strings cannot be reassigned while pointers can. Finally, it lists and briefly explains other standard string library functions.
This document contains an interview with Colin Dean, a computer science graduate. In the interview, Dean discusses his realization that he wanted to study computer science, provides advice for first-year students, and shares details about his education and career. He also answers questions about whom he would like to have dinner with, what technology blogs he reads, and where he sees himself in ten years. The document concludes with biographical information about Colin Dean.
watch a video here for beginneers
in c++ programming language
https://www.youtube.com/channel/UCThawdb6qPuk3wkCtinhLDg
This document provides an overview of fundamental computer programming concepts in Chapter 1. It begins with defining what a computer program and programming are, and reasons for studying programming such as career opportunities and developing logical thinking. It then covers the program development life cycle and an overview of programming languages and paradigms. The remainder of the document discusses specific aspects of C++ programs including compilation processes, program structure, input/output streams, library functions, preprocessor directives, variables and data types.
watch a video here for beginneers
in c++ programming language
https://www.youtube.com/channel/UCThawdb6qPuk3wkCtinhLDg
watch a video here for beginneers
in c++ programming language
https://www.youtube.com/channel/UCThawdb6qPuk3wkCtinhLDg
watch a video here for beginneers
in c++ programming language
https://www.youtube.com/channel/UCThawdb6qPuk3wkCtinhLDg
How to Setup Renewal of Subscription in Odoo 18Celine George
A subscription is a recurring plan where you set a subscription period, such as weekly, monthly, or yearly. Based on this period, the subscription renews automatically. In Odoo 18, you have the flexibility to manage renewals either manually or automatically.
Happy Summer Everyone. This is also timeless for future viewing.
You all have been upgraded from ‘Guest’ Students to ‘Graduate’ Students. Do Welcome Back. For new guests, please see our free weekly workshops from Spring ‘25’
Blessings, Love, and Namaste’.
Do Welcome to Summer ‘25’ for LDMMIA.
TY, for surviving our First Season/Term of our Reiki Yoga Workshops. These presentations/workshop are designed for your energy wellness.
Also, professional expansion for Summer ‘25’. All updates will be uploaded here and digital notes within our Merch Shop. (I am Completely, using the suggestions of AI for my Biz style. Its spooky accurate. So far, AI has been very helpful for office and studio admin. I even updated my AI avatars. Similar to my SL Meta avatar.)
Do take Care of yourselves. This is only a Bonus Checkin. The Next Workshop will be Lecture/Session 8. I will complete by Friday.
https://ldm-mia.creator-spring.com/
Search Engine Optimization (SEO) for Website SuccessMuneeb Rana
Unlock the essentials of Search Engine Optimization (SEO) with this concise, visually driven PowerPoint. Inside you’ll find:
✅ Clear definitions and core concepts of SEO
✅ A breakdown of On‑Page, Off‑Page, and Technical SEO
✅ Actionable best‑practice checklists for keyword research, content optimization, and link building
✅ A quick‑start toolkit featuring Google Analytics, Search Console, Ahrefs, SEMrush, and Moz
✅ Real‑world case study demonstrating a 70 % organic‑traffic lift
✅ Common challenges, algorithm updates, and tips for long‑term success
Whether you’re a digital‑marketing student, small‑business owner, or PR professional, this deck will help you boost visibility, build credibility, and drive sustainable traffic. Download, share, and start optimizing today!
Coleoptera, commonly known as beetles, is the largest order of insects, comprising approximately 400,000 described species. Beetles can be found in almost every habitat on Earth, exhibiting a wide range of morphological, behavioral, and ecological diversity. They have a hardened exoskeleton, with the forewings modified into elytra that protect the hind wings. Beetles play important roles in ecosystems as decomposers, pollinators, and food sources for other animals, while some species are considered pests in agriculture and forestry.
RELATIONS AND FUNCTIONS
1. Cartesian Product of Sets:
If A and B are two non-empty sets, then their Cartesian product is:
A × B = {(a, b) | a ∈ A, b ∈ B}
Number of elements: |A × B| = |A| × |B|
2. Relation:
A relation R from set A to B is a subset of A × B.
Domain: Set of all first elements.
Range: Set of all second elements.
Codomain: Set B.
3. Types of Relations:
Empty Relation: No element in R.
Universal Relation: R = A × A.
Identity Relation: R = {(a, a) | a ∈ A}
Reflexive: (a, a) ∈ R ∀ a ∈ A
Symmetric: (a, b) ∈ R ⇒ (b, a) ∈ R
Transitive: (a, b), (b, c) ∈ R ⇒ (a, c) ∈ R
Equivalence Relation: Reflexive, symmetric, and transitive
4. Function (Mapping):
A relation f: A → B is a function if every element of A has exactly one image in B.
Domain: A, Codomain: B, Range ⊆ B
5. Types of Functions:
One-one (Injective): Different inputs give different outputs.
Onto (Surjective): Every element of codomain is mapped.
One-one Onto (Bijective): Both injective and surjective.
Constant Function: f(x) = c ∀ x ∈ A
Identity Function: f(x) = x
Polynomial Function: e.g., f(x) = x² + 1
Modulus Function: f(x) = |x|
Greatest Integer Function: f(x) = [x]
Signum Function: f(x) =
-1 if x < 0,
0 if x = 0,
1 if x > 0
6. Graphs of Functions:
Learn shapes of basic graphs: modulus, identity, step function, etc.
Types of Actions in Odoo 18 - Odoo SlidesCeline George
In Odoo, actions define the system's response to user interactions, like logging in or clicking buttons. They can be stored in the database or returned as dictionaries in methods. Odoo offers various action types for different purposes.
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdfTechSoup
In this webinar we will dive into the essentials of generative AI, address key AI concerns, and demonstrate how nonprofits can benefit from using Microsoft’s AI assistant, Copilot, to achieve their goals.
This event series to help nonprofits obtain Copilot skills is made possible by generous support from Microsoft.
Students will research and orally present a Colombian company using a visual tool, in order to develop their communication skills and intercultural understanding through the exploration of identity, innovation, and local culture, in connection with the IB global themes.
This study describe how to write the Research Paper and its related issues. It also presents the major sections of Research Paper and various tools & techniques used for Polishing Research Paper
before final submission.
Finding a Right Journal and Publication Ethics are explain in brief.
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...RVSPSOA
Principles of statics. Forces and their effects. Types of force systems. Resultant of concurrent and
parallel forces. Lami’s theorem. Principle of moments. Varignon’s theorem. Principle of equilibrium.
Types of supports and reactions-Bending moment and Shear forces-Determination of reactions for
simply supported beams. Relation between bending moment and shear force.
Properties of section – Centre of gravity, Moment of Inertia, Section modulus, Radius of gyration
for various structural shapes. Theorem of perpendicular axis. Theorem of parallel axis.
Elastic properties of solids. Concept of stress and strain. Deformation of axially loaded simple bars.
Types of stresses. Concept of axial and volumetric stresses and strains. Elastic constants. Elastic
Modulus. Shear Modulus. Bulk Modulus. Poisson’s ratio. Relation between elastic constants.
Principal stresses and strain. Numerical and Graphical method. Mohr’s diagram.
R.K. Bansal, ‘A Text book on Engineering Mechanics’, Lakshmi Publications, Delhi,2008.
R.K. Bansal, ‘A textbook on Strength of Materials’, Lakshmi Publications, Delhi 2010.
Paul W. McMullin, 'Jonathan S. Price, ‘Introduction to Structures’, Routledge, 2016.
P.C. Punmia, ‘Strength of Materials and Theory of Structures; Vol. I’, Lakshmi
Publications, Delhi 2018.
2. S. Ramamrutham, ‘Strength of Materials’, Dhanpatrai and Sons, Delhi, 2014.
3. W.A. Nash, ‘Strength of Materials’, Schaums Series, McGraw Hill Book Company,1989.
4. R.K. Rajput, ‘Strength of Materials’, S.K. Kataria and Sons, New Delhi , 2017.
Order Lepidoptera: Butterflies and Moths.pptxArshad Shaikh
Lepidoptera is an order of insects comprising butterflies and moths. Characterized by scaly wings and a distinct life cycle, Lepidoptera undergo metamorphosis from egg to larva (caterpillar) to pupa (chrysalis or cocoon) and finally to adult. With over 180,000 described species, they exhibit incredible diversity in form, behavior, and habitat, playing vital roles in ecosystems as pollinators, herbivores, and prey. Their striking colors, patterns, and adaptations make them a fascinating group for study and appreciation.
Jack Lutkus is an education champion, community-minded innovator, and cultural enthusiast. A social work graduate student at Aurora University, he also holds a BA from the University of Iowa.
POS Reporting in Odoo 18 - Odoo 18 SlidesCeline George
To view all the available reports in Point of Sale, navigate to Point of Sale > Reporting. In this section, you will find detailed reports such as the Orders Report, Sales Details Report, and Session Report, as shown below.
How to Setup Lunch in Odoo 18 - Odoo guidesCeline George
In Odoo 18, the Lunch application allows users a convenient way to order food and pay for their meal directly from the database. Lunch in Odoo 18 is a handy application designed to streamline and manage employee lunch orders within a company.
How to Create a Stage or a Pipeline in Odoo 18 CRMCeline George
In Odoo, the CRM (Customer Relationship Management) module’s pipeline is a visual representation of a company's sales process that helps sales teams track and manage their interactions with potential customers.
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfChalaKelbessa
This is Forestry Exit Exam Model for 2025 from Department of Forestry at Wollega University, Gimbi Campus.
The exam contains forestry courses such as Dendrology, Forest Seed and Nursery Establishment, Plantation Establishment and Management, Silviculture, Forest Mensuration, Forest Biometry, Agroforestry, Biodiversity Conservation, Forest Business, Forest Fore, Forest Protection, Forest Management, Wood Processing and others that are related to Forestry.
IDSP is a disease surveillance program in India that aims to strengthen/maintain decentralized laboratory-based IT enabled disease surveillance systems for epidemic prone diseases to monitor disease trends, and to detect and respond to outbreaks in the early phases swiftly.....
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...Arshad Shaikh
Dictyoptera is an order of insects that includes cockroaches and praying mantises. These insects are characterized by their flat, oval-shaped bodies and unique features such as modified forelegs in mantises for predation. They inhabit diverse environments worldwide.
4. 4) Basic of String
What is string?
A string is a collection of characters .
It is usually a meaningful sequence representing the name of
an entity.
Generally it is combination of two or more characters
enclosed in double quotes.
Example:
“Good Morning” // string with 2 words
“Mehak” // string with one word
”B.P.” // string with letters and symbols
“” // an empty string
The above examples are also known as string literals
Chapter 4
4
5. 4) Basic of String (cont’d)
String in C++
C++ does not provide with a special data type to store strings.
• Thus we use arrays of the type char to store strings
Strings in C++ are always terminated using a null character
(‘0’)
Strings can be one dimensional or multi- dimensional
character arrays terminated by a null character (‘0’)
String literals are the values stored in the character array
Example: "Hi there!“
===> would be stored in memory as shown:
Chapter 4
5
H i t h e r e ! 0
6. 4.1) String Declaration (C Style)
To declare a character array (string) the syntax is
char stringName[size]; //One dimensional string
char stringName[rSize] [cSize]; //Two dimensional string
Example: char name[20];
char stud_Name [30][20];
Note:
Here name and stud_name is a character array or string capable of
storing maximum of 19 characters and 570 characters respectively.
Since one character is reserved for storing ‘0’, the number of elements
that can be stored in a 1D string is always size-1
Incase of 2D string each row should ends with ‘0’ and the maximum
number of characters that will stored is total_size – row_Size
Chapter 4
6
rsize: no of strings
cSize: size of each string
7. 4.1) String Declaration (C++ Style)
In C++ a string can be declared with string object in addition to that
of C-style declaration.
Example:
string myString;
string city, country; string address[10];
C-strings vs. string objects
Chapter 4
7
C-strings string objects
Implemented as arrays of type char Instance of string class
Terminated with the null character Not terminated with null character
Compile-time allocation and
determination of size
run-time allocation and undetermined
size
Fixed size, but do not track their own
size
Dynamic size and also track their own
size
8. 4.1) String Declaration (C++ Style)
Unlike C-style string, the string class library used to declared a
string as regular variables (not as arrays), and they support:
The assignment operator =
Comparison operators ==, !=, etc
The + operator for concatenation
Type conversions from c-strings to string objects
A variety of other member functions
Chapter 4
8
9. 4.2) String initialization
(a) 1D String Initialization
Example 1:
char name[20] = “John Eyob”;
- The above string will have 9 characters and 1 space for the null.
Thus size of name will be 10.
Example 2: string name = “John Eyob”;
Chapter 4
9
string
name[0] name[1]
name[7] name[9]
null
character
J o h n E y . .
. . . . . . . . . . . . . . .
o b ‘0
10. 4.2) String initialization (cont’d)
Example 2: omitting string size
- Like as we do in array string size can be omitted also
char myAddress[] = “Addis Ababa, Ethiopia”;
In this case the string is initialized to the mentioned string literal and it’s
size is the number of characters in the string literal plus null character.
Here it is 20.
The null character is automatically inserted at the end of the string.
Example 3: initializing string character by character
char city [10] = {‘A’, ‘d’, ‘a’, ‘m’, ‘a’, ‘0’};
char myCity [ ] = {‘D’, ‘i’, ‘r’, ‘e’, ‘d’, ‘e’, ‘w’, ‘a’, ‘0’};
Note: The ‘0’ has to be inserted by the programmer.
Chapter 4
10
11. 4.2) String initialization (cont’d)
Some more examples of string initialization
Chapter 4
11
Initialization Memory representation
char animal[7]=“Lion”;
char location[]=“Aksum City”;
char serial_no[]=“A011”;
char name [5] = “Gamechis”; //invalid, out of bound
char company[10] =
“Ethiotel”;
char country [] = ‘Ethioipia’; //invalid, string must enclosed within
double quote
A k s u m C i t y ‘0’
A 0 1 1 ‘0’
L i o n ‘0’
E t h i o t e l ‘0’
12. 4.2) String initialization (cont’d)
(b) Initializing 2 D Strings - 2D string can be initialize as follows
Example 1:
char word[4][5]={“Cat”, “Boat”, “Mat”,”Rate”};
12
13. 4.2) String initialization (cont’d)
Example 2: Omitting string rowSize (number of strings)
char name[][12] = {"Mr. Biniam", "Mr. Abush",
“Miss Nardos", “Mrs. Kidest",
“Dr. Andualem", “Eng. Yodit"};
- #strings (rowSize) = 6
13
M r . B i n i a m 0
M r . A b u s h 0
M i s s N a r d o s 0
M r s . K i d e s t 0
D r . A n d u a l e m 0
E n g . Y o d i t 0
Columns
0 1 2 3 4 5 6 7 8 9 10 11 12
5
4
3
2
1
0
rows
name[2][2]
2nd index 1st
index
name[5][7] name[4][8]
15. 4.2) String initialization (cont’d)
Example 6: initializing string after declaration
char mystring [6];
mystring[0] = 'H'; mystring[1] = 'e';
mystring[2] = 'l'; mystring[3] = 'l';
mystring[4] = 'o'; mystring[5] = '0';
Note: Like wise 2D strings can be initialized after declaration.
Example 7: Invalid string initialization/assignment
char mystring[6];
mystring="Hello"; // not allowed
mystring[] = "Hello"; //illegal
mystring = { 'H', 'e', 'l', 'l', 'o', '0' }; //neither would be valid
Chapter 4
15
16. 4.3) String input/output
A string is displayed using a simple cout<< stream statement
However, input a string or character array can be performed through
any one of the following
Chapter 4
16
No Input method Descriptions
1 cin>> stream
• Inputs a string without spaces
• The >> operator stops input when it encounters a
space
• Syntax: cin>>str;
2 get() function
• Used to input either single character or a line of text
with spaces
• Syntax 1: cin.get(ch);
where ch is a character
• Syntax 2: cin.get(str, n);
where str is string and n specify the size of
string to be read.
17. 4.3) String input/output (cont’d)
Chapter 4
17
No Input method Descriptions
3 gets() function
• Can be used to input a single line of text including
spaces.
• As soon as the enter is pressed it stops input
• Syntax: gets( str );
where str is a string
4 getline() function
• Can be used to input multiple lines of text.
• Syntax: cin.getline(string, MAX, Delimiter)
were - String is the character array
- Max is the maximum number of characters
allowed input
- Delimeter is the character which when
encountered in the input stream
stops the input
Note: it is no needed to use loop to input or display a string unless the character
array (string) is 2D and we need to read/print multiple strings.
19. 4.3) String input/output (cont’d)
Note:
The getline function continues to input the string until either the maximum
number of characters are input or it encounters the delimiter character
whichever comes first.
Chapter 4
19
20. 4.4) String Operation/manipulations
Assignment/copy and comparison operation
In C-style, strings cannot be copied or compared using the
simple assignment or comparison operator as follow.
char str1[20], str2[20];
str2=str1; // Not allowed
if(str1==str2) //Not allowed
{ ….. }
However, using the C++ string objects the above two string
operations are valid
str2=str1; if(str1==str2) //both are valid
Chapter 4
20
21. 4.4) String Operation/manipulations (cont’d)
Assignment/copy and comparison operation
In C-style, strings cannot be copied or compared using the
simple assignment or comparison operator as follow.
char str1[20], str2[20];
str2=str1; // Not allowed
if(str1==str2) //Not allowed
{ ….. }
However, using the C++ string objects the above two string
operations are valid
str2=str1; if(str1==str2) //both are valid
Chapter 4
21
22. 4.4) String Operation/manipulations (cont’d)
Other string operations
Find the string length
Search string or substring
Characters case conversion
Reverse or swap string
Concatenating strings
String tokenization etc.
Modifying (replace) string
The above mentioned string manipulations can be performed
either through hard coding or using library functions
Chapter 4
22
23. 4.4) String Manipulations and Library Functions
Here below list of string manipulation library functions
Chapter 4
23
Description
Function
String
operations
Copies string str2 (source string) into
the character array str1 (destination
string). The value of str1 is returned.
strcpy(str1, str2);
String copying
Copies at most n characters of string
s2 into the array s1. The value of s1
is returned.
strncpy(str1, str2, size_t n);
Appends string s2 to string s1. The
value of s1 is returned.
strcat (str1, str2);
String
concatenation Appends at most n characters of
string s2 to string s1. The value of s1
is returned.
strncat (str1, str2, size_t n);
24. 4.4) String Library Functions (cont’d)
Here below list of string manipulation library functions
Chapter 4
24
Description
Function
String
operations
Compares string str1 with string str2. The
function returns a value of
• zero, if str1 is equal to str2
• less than zero, if str1 is less than str2
• greater than zero, if str1 greater than
str2
strcmp(str1, str2);
String
comparison
Compares up to n characters of string str1
with string str2. It works in the fashion as
strcmp().
strncmp(str1, str2, size n);
Compares string str1 with string str2 in
regardless of their cases (upper case or
lower case.
int stricmp(str1, str2);
Compares up to n characters of string str1
with string str2 in regardless of their cases
strnicmp(str1, str2, size n);
25. 4.4) String Library Functions (cont’d)
Here below list of string manipulation library functions
Chapter 4
25
Description
Function
String operations
Determines the length of string str. The
number of characters preceding the
terminating null character is returned.
strlen(str);
String length
Returns a the first left occurrence of
character ch in string str1.
strch(str1, ch);
Looking for string /
character
Occurrence
Returns a the first right occurrence of
character ch in string str1 .
strrch(str1, ch);
Returns a the first occurrence of string str2
in string str1.
strstr(str1, str2);
Converts lowercase characters in strings to
uppercase
strupr(str1)
String case
conversion Converts uppercase characters in strings to
lowercase
strlwr(str1)
26. 4.4) String Library Functions (cont’d)
Here below list of string manipulation library functions
Chapter 4
26
Description
Function
String
operations
finds up at what length two strings are identical
strspn(str1, str2)
Others
Reversing all characters of a string
strrev( str )
A sequence of calls to strtok breaks string str1
into “tokens”—logical pieces such as words in a
line of text—delimited by characters contained
in string s2.
The first call contains str1 as the first argument,
and subsequent calls to continue tokenizing the
same string contain NULL as the first argument
strtok( str1, s2 );
Repalcae character(s) of string to a given
character
strset(str, ch),
strnset(str, ch, 5)
Note: These are some of the library functions and Many More are available
27. 4.4) String Library Functions (cont’d)
Relational Operators and library functions
supported by C++ String objects
Chapter 4
27
functions Descriptions
append() appends a part of a string to another string
assign() assigns a partial string
at() obtains character stored at a specified
location
begin() returns a reference to the start of the string
capacity() gives the total element that can be stored
compare() compares a string against the invoking string
empty() returns true if the string is empty
end() returns a reference to the end of the string
erase() removes character as specified
find() searches for the occurrence of a specified
substring
swap() swaps the given string with the invoking on
28. 4.4) String Library Functions (cont’d)
Correspondence between
the C-library and the C++ string class/object
Chapter 4
28
29. 4.4) String Library Functions (cont’d)
Character handling library functions of ctype.h
Chapter 4
29
Prototype Description
isdigit(c ) Returns true if c is a digit and false otherwise
isalpha( c ) Returns true if c is a letter and false otherwise
isalnum( c ) Returns true if c is a digit/letter and false otherwise
isxdigit( c ) Returns true if c is a hexadecimal digit and false otherwise
islower( c) Returns true if c is a lowercase letter and false otherwise
isupper( c) Returns true if c is an uppercase letter; false otherwise
tolower( c ),
toupper( c )
If c is an uppercase letter, it returns c as a lowercase letter. Otherwise,
leave the character/string unchanged and vice versa
isgraph( c ) Returns true if c is a printing character other than space (' ')
isspace(c )
Returns true if c is a white-space, newline ('n'), space (' '), form feed ('f'),
carriage return ('r'), horizontal tab ('t'), or vertical tab ('v') and false
otherwise
iscntrl( c ) Returns true if c is a control character and false otherwise
ispunct( c )
Returns true if c is a printing character other than a space, a digit, or a
letter and false otherwise
isprint( c ) Returns true value if c is a printing character including space (' ')
30. 4.4) String manipulation (cont’d)
Example 1: string length
Chapter 4
30
(a) Hard coding
(b) Using strlen() library function
32. 4.4) String manipulation (cont’d)
Example 3: copying, concatenate and compare using library functions
Chapter 4
32
(b) String comparison
copying and concatenating
the 1st nth string
Comparing the 1st nth
characters of strings
33. 4.4) String manipulation (cont’d)
Example 4: more on string manipulations
Chapter 4
33
String case conversion
Reverse string
35. 4.4) String manipulation (cont’d)
Example 6: Program to display the words which start with a capital ‘A’
Chapter 4
35
36. Practical Exercises 2 - Strings
1. Write a program to count total number of vowels and consonants
present in a string.
2. Design a program to find the frequency of characters within string and
display character with largest and smallest frequency respectively.
3. Write a program that find the frequency of vowel, consonant, digit and
special character
4. Design a program to check either the word is palindrome or not using
loop.
5. Write a program to remove non-alphabet character from string
6. Write a program to store and print the names of your two favorite
television programs. Store these programs in two character arrays.
Initialize one of the strings (assign it the first program’s name) at the
time you declare the array. Initialize the second value in the body of
the program with the strcpy() function.
7. Write an application that inputs a line of text and outputs the text
twice, once in all uppercase and once in all lowercase letters.
Chapter 2
36
37. Reading Resources/Materials
Chapter 13:
Diane Zak; An Introduction to Programming with C++ (8th Edition),
2016 Cengage Learning
Chapter 8:
Walter Savitch; Problem Solving With C++ [10th edition,
University of California, San Diego, 2018
Link:
https://www.w3schools.in/category/cplusplus-tutorial/
37