SDLC
SRAM vs DRAM: Know the Difference
Before learning about SDRAM and DRAM first, we need to understand about the RAM What is RAM? The...
A string is a sequence of characters. A C++ string is an object of the std::string class. The characters are stored sequences of bytes with access to a single character byte allowed.
C++ strings allocate memory dynamically. More memory can be allocated to the string during run time if needed. Since there is no memory pre-allocation, no wastage of memory. We can perform various operations on strings, including comparisons, concatenation, conversion, etc.
In this C++ tutorial, you will learn:
C++ supports two types of string declarations:
This type of string declaration was introduced in C programming language. C++ continues to support it. It's simply a one-dimensional array of characters terminated with a null character (\0). A null-terminated string has characters that make up the string then followed by a null.
Consider the string declaration given below:
char name[5] = {'J', 'o', 'h', 'n', '\0'};
The above declaration creates a string that forms the word John. The word has 4 characters, but the string has a size of 5. The extra space allows for holding of the null character.
Using the array initialization rule, we can write the above statement as follows:
char name[] = "John";
Note that you don't have to place the null character at the end of the string constant. The C++ compiler will automatically place the '\0' at the string's end when initializing the array.
The standard C++ library provides the string class which supports various string operations. It is written as std::string.
To use this class, we must first include it into our workspace using the #include preprocessor as shown below:
#include<string>
Next, we can declare our string using the string keyword. For example:
string name = "John";
The above statement will create a string named name to hold the value John.
In C++, we can access the string values using the string name. For example:
#include <iostream>
using namespace std;
int main() {
char name[5] = { 'J', 'o', 'h', 'n', '\0' };
cout << "String value is: ";
cout << name << endl;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
Here is another example using the C++ standard string class:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "gtupapers";
cout << "The name is : " << name << endl;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
You will often want to manipulate strings. C++ provides a wide range of functions that you can use for this. These functions are defined in the CString class, hence, we have to include it in our code in order to use the functions. Let us discuss some:
This is the string copy function. It copies one string into another string.
Syntax:
strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings. The function will copy the string string1 into the string 1.
This is the string concatenate function. It concatenates strings.
Syntax:
strcat(string1, string2);
The two parameters to the function, string1 and string2 are the strings to be concatenated. The above function will concatenate the string string2 to the end of the string string1.
This is the string length function. It returns the length of the string passed to it as the argument.
Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be determined. The above function will return the length of the string string1.
This is the string compare function. It is used for string comparison.
Syntax:
strcmp(string1, string2);
The above function will return 0 if strings string1 and string2 are similar, less than 0 if string1<string2 and greater than 0 if string1>string2.
The following example demonstrates how to use the above string functions:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char name1[10] = "gtupapers";
char name2[10] = "John";
char name3[10];
int len;
strcpy(name3, name1);
cout << "strcpy( name3, name1) : " << name3 << endl;
strcat(name1, name2);
cout << "strcat( name1, name2): " << name1 << endl;
len = strlen(name1);
cout << "strlen(name1) : " << len << endl;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
Before learning about SDRAM and DRAM first, we need to understand about the RAM What is RAM? The...
In this tutorial of difference between AR and VR, we will discuss the key difference between...
iPhone Recovery Software are applications that allow you to bring back your lost data from...
Video quality enhancers are tools that enable you to improve the resolution of a video. These...
What is Hadoop? Apache Hadoop is an open source software framework used to develop data processing...
An email app for iPhone enables you to configure email addresses to receive, read, compose, and...