C++ Mam
C++ Mam
C++ Mam
1|Pag e
C++ Strings
One of the most useful data types supplied in the C++ libraries is the string. A string
is a variable that stores a sequence of letters or other characters, such as "Hello" or
"May 10th is my birthday!". Just like the other data types, to create a string we
first declare it, then we can store a value in it.
String testString;
testString = "This is a string.";
Often, we use strings as output, and cout works exactly like one would expect:
In order to use the string data type, the C++ string header <string> must be included
at the top of the program. Also, you’ll need to include genlib.h to make the short
name string visible instead of requiring the cumbersome std::string. (As a side
note, std is a C++ namespace for many pieces of functionality that are provided in
standard C++ libraries. For the purposes of this class, you won't need to know about
namespaces.) Thus, you would have the following #include's in your program in
order to use the string type.
#include <string>
#include "genlib.h"
Basic Operations
2|Pag e
Accessing individual characters. Using square brackets, you can access
individual characters within a string, similar to as if the string were an array. Just as
with array access, positions are numbered starting from 0, and the last position is
(length - 1). Using square brackets, you can both read the character at a position
and assign to that position.
Be careful not to access positions outside the bounds of the string! The square
bracket operator is not range-checked (for efficiency reasons) and thus reading from
or writing to an out-of-bounds index tends to produce difficult-to-track-down errors.
There is an alternate member function at (index) that retrieves the character at a
position with the benefit of built-in range-checking.
3|Pag e
Similarly, passing and returning strings from functions copies the string contents. If
you change a string parameter within a function, changes are not seen in the calling
function unless you have specifically passed the string by reference.
Comparing two strings: You can compare two strings for equality using the ordinary
== and!= operators (just like they were ints). Suppose you ask the user for his or her
name.
If the user is Julie, the program prints a warm welcome. If the user is not Neal, the
program prints the normal message. Finally… if the user is Neal, it prints a less
enthusiastic response.
You can use <, <=, >, and >= to order strings. These operators compare strings
lexicographically, character by character and are case-sensitive. The following
comparisons all evaluate to true: "A" < "B", "App" < "Apple", "help" > "hello",
"Apple" < "apple". The last one might be a bit confusing, but the ASCII value for 'A'
is 65, and comes before 'a', whose ASCII value is 97. So "Apple" comes before
"apple" (or, for that matter, any other word that starts with a lower-case letter).
Appending to a string: C++ strings are wondrous things. Suppose you have two
strings, s1 and s2 and you want to create a new string of their concatenation.
Conveniently, you can just write s1 + s2, and you’ll get the result you’d expect.
Similarly, if you want to append to the end of string, you can use the += operator.
You can append either another string or a single character to the end of a string.
4|Pag e
C++ string class and its applications
string class
String class is part of C++ library that supports a lot much functionality over C style
strings. C++ string class internally uses char array to store character but all memory
management, allocation and null termination is handled by string class itself that is
why it is easy to use. The length of c++ string can be changed at runtime because of
dynamic allocation of memory similar to vectors. As string class is a container class,
we can iterate over all its characters using an iterator similar to other containers like
vector, set and maps, but generally we use a simple for loop for iterating over the
characters and index them using [] operator.
C++ string class has a lot of functions to handle string easily. Most useful of them are
demonstrated in below code.
PROGRAM
Write a program to find the length of string.
Source Code
#include<iostream>
using namespace std;
int main( )
{
char str[80];
5|Pag e
cout<<"Enter string: ";
cin.getline(str, 80);
return 0;
}
Output
Source Code
#include<iostream>
using namespace std;
int main( )
{
char str[80];
for(int i=0;str[i]!='\0';i++)
{
str[i] = (str[i] >= 'A' && str[i] <= 'Z') ? (str[i] + 32) : str[i];
}
return 0;
}
(GeeksOfGeek, 2010)
Output
6|Pag e
Enter string: Hello World
Lowercase string: hello world
7|Pag e
Bibliography
CodesCracker. (n.d.). Retrieved from CodesCracker: https://codescracker.com/cpp/index.htm
GeeksOfGeek. (2010). Retrieved from Geek of Geek: https://www.geeksforgeeks.org/c-plus-plus/
Learn c++. (n.d.). Retrieved from Learn c++: https://www.programiz.com/cpp-programming
8|Pag e