0% found this document useful (0 votes)
4 views12 pages

Lecture 5

The document discusses string handling in C++, covering both C-style strings and the C++ string class. It explains initialization, input considerations, and various functions available for string manipulation. Additionally, it provides code links for practical examples and demonstrates how to pass strings to functions and use string iterators.

Uploaded by

Adnaan Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Lecture 5

The document discusses string handling in C++, covering both C-style strings and the C++ string class. It explains initialization, input considerations, and various functions available for string manipulation. Additionally, it provides code links for practical examples and demonstrates how to pass strings to functions and use string iterators.

Uploaded by

Adnaan Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming

Paradigms in C++
CSCI 3142

By

Priyanka Samanta
String
• C++ supports two forms of strings:
• C style strings and
• C++ string class

A C-style string is literally an array of char with a terminating null, '\0’


char good[6]= { 'h', 'e', 'l', 'l', 'o', '\0' };
char bad[6] = { 'h', 'e', 'l', 'l', 'o', ' ‘ };

•This null character marks the end of a C-style string, and many string
processing functions depend on the presence of this marker to do their work.
A better way to initialize a char array
You may not like always having to worry about adding the null char
•String literals
char iknowmysize[6] = “hello”; // only 5 chars, null char added implicitly
char letcompilercount[] = “hello”; // either 6 or above, or leave blank

•Notice ‘S’ and “S” are different


•The former is a single character
•The latter is a string (represented by its address), and can’t be assigned to
a char type
Continue
•Any string literals separated by whitespaces (spaces, tabs, and newlines) are
merged automatically as one single string
char longstr[] = “this is a very long string”
“ which can be broken up this way”;
• Using a pointer: char *s=“Hello”; //behave as const char *
• Using string class: string s=“Hello”;

Code link: https://replit.com/@SamantaPriyanka/Strings#main.cpp


Consideration when inputting string

Two issues when reading strings from keyboard


•When using cin to read strings from the keyboard, it stops at
each whitespace, so names such “New York” will be broken up
•It’s hard to predetermine how big a char array to receive the
string

Code link: https://replit.com/@SamantaPriyanka/Strings#stringInput.cpp


C++ String to read a line of text
• To read the text containing blank space, cin.get() function can be used. This
function takes two arguments.

• First argument is the name of the string (address of first element of string) and
second argument is the maximum size of the array.

Code link: https://replit.com/@SamantaPriyanka/Strings#stringInput1.cpp


string Object
• In C++, you can also create a string object for holding strings.
• Unlike using char arrays, string objects has no fixed length, and can be extended
as per your requirement.
• Instead of using cin>> or cin.get() function, you can get the entered line of text
using getline().
• getline() function takes the input stream as the first parameter which is cin and
str as the location of the line to be stored.

Code link: https://replit.com/@SamantaPriyanka/Strings#stringInput2.cpp


Passing String to a Function
• Strings are passed to a function in a similar way arrays are passed to a
function.

Code link: https://replit.com/@SamantaPriyanka/Strings#stringFunction.cpp


Functions in string.h/cstring
1. strlen(str1)
2. strcat(dest,source)
3. strncat(dest,source,length) //what length of the source want to concat
4. strcpy(dest,source)
5. strncpy(dest,source,length)
6. strstr(main,sub) //display the starting from common portion upto end of the string
7. strchr(main,char) //check the occurrence of char
8. strrchr(main,char) //check from the rear end
9. strcmp(str1,str2) //return +ve, 0 or –ve

code link: https://replit.com/@SamantaPriyanka/Strings#cstringFunctions.cpp


Basic functions of string class
1. s.length()
2. s.size()
3. s.capacity()
4. s.resize()
5. s.max_size()
6. s.clear()
7. s.empty
8. s.append(“Bye”)
9. s.insert(3, “kk”)
10. s.replace(3, 5,“aa”)
Basic functions of string class
1. s.push_back(‘z’)
2. s.pop_back()
3. s1.swap(s2)
4. s.copy(char des[])
5. s.find(str or char)
6. s.rfind(str)
7. s.substring(start,number)
8. s.compare(str)
Replit link: https://replit.com/@SamantaPriyanka/Strings#stringClassFunction.cpp
string class-iterator
string::iterator
1. begin()
2. end()
3. reverse_iterator()
4. rbegin()
5. rend()
code link: https://replit.com/@SamantaPriyanka/Strings#stringIterator.cpp

You might also like