C++ Programming Code Examples
Learn C++ Language
String Library substr() Function in C++ Programming Language
String Library substr() Function in C++
Generate substring. Returns a newly constructed string object with its value initialized to a copy of a substring of this object. The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).
In C++, std::substr() is a predefined function used for string handling. This function takes two values pos and len as an argument and returns a newly constructed string object with its value initialized to a copy of a sub-string of this object. Copying of string starts from pos and is done till pos+len means [pos, pos+len).
Syntax for String substr() Function in C++
#include <string>
string substr (size_t pos = 0, size_t len = npos) const;
pos
Position of the first character to be copied as a substring.
If this is equal to the string length, the function returns an empty string.
If this is greater than the string length, it throws out_of_range.
The first character is denoted by a value of 0 (not 1).
len
Number of characters to include in the substring (if the string is shorter, as many characters as possible are used).
A value of string::npos indicates all characters until the end of the string.
size_t is an unsigned integral type (the same as member type string::size_type).
Function returns a string object with a substring of this object.
• The index of the first character is 0 (not 1).
• If pos is equal to the string length, the function returns an empty string.
• If pos is greater than the string length, it throws out_of_range. If this happens, there are no changes in the string.
• If the requested sub-string len is greater than the size of a string, then returned sub-string is [pos, size()).
Complexity
Unspecified, but generally linear in the length of the returned object.
Iterator validity
No changes
Data races
The object is accessed
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the string.
If pos is greater than the string length, an out_of_range exception is thrown.
A bad_alloc exception is thrown if the function needs to allocate storage and fails.
/* string substr() function is used for handling string operations. It generates a new string with its value initialized to a copy of a sub-string of this object. */
#include <iostream>
using namespace std;
int main() {
string str1 = "I Love Clementine";
int start = 5;
int len = 10;
char substring[len+1];
for(int i=0; i < len; i++) {
substring[i] = str1[start + i];
}
//end string
substring[len] = '\0';
cout << substring;
}
C++ Language program sample which passes a "Reference" to a Function and Function has a 'pointer' as argument. Keep the return type void & display result in main body. Apply any
Array is the collection of similar data type, In this c++ program we find duplicate elements from an array, Suppose array have 3, 5, 6, 11, 5 and 7 elements, in this array 5 appear two
Give the degree of the polynomiala. Give the value of the coefficients. Give the value of the coefficients. Give the values of the coefficient of polynomialb. Give value to be substituted
First think what is the factorial of a number? How 'mathematically' it can be calculated. If you got this info then it will be very easier to make a C++ code logic to find the "Factorial".