...

Programming languages - C++

Back to Course

Lesson Description


Lession - #47 C++ Strings


C++ gives following two sorts of string portrayals −
 

  • The C-style character string.
  • The string class type presented with Standard C++.

The C-Style Character String

The C-style character string began inside the C language and keeps on being upheld inside C++. This string is really a one-dimensional array of characters which is ended by an null character '\0'. Hence an null-terminated string contains the characters that include the string followed by an invalid.

The accompanying announcement and introduction make a string comprising of "Hi". To hold the null character toward the finish of the array, the size of the character array containing the string is one more than the quantity of characters in "Hi."

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

 


If you follow the rule of array initialization, then you can write the above statement as follows −

 

 

char greeting[] = "Hello";


Following is the memory presentation of above defined string in C/C++ −


Let us try to print above-mentioned string −

 

 

#include 

using namespace std;

int main (>
{ char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; cout << "Greeting message: "; cout << greeting << endl; return 0; }

 


C++ supports a wide range of functions that manipulate null-terminated strings −

 

 

Sr.No Function and Purpose
1 strcpy(s1, s2>
;
Duplicates string s2 into string s1.
2 strcat(s1, s2>
;
Concatenates string s2 onto the finish of string s1.
3 strlen(s1>
;
Returns the length of string s1.
4 strcmp(s1, s2>
;
Returns 0 if s1 and s2 are something similar; under 0 if s1s2.
5 strchr(s1, ch>
;
Returns a pointer to the main event of character ch in string s1.
6 strstr(s1, s2>
;
Returns a pointer to the primary event of string s2 in string s1.

 


Following example makes use of few of the above-mentioned functions −

 

 

#include 
#include 

using namespace std;

int main (>
{ char str1[10] = "Hello"; char str2[10] = "World"; char str3[10]; int len ; // copy str1 into str3 strcpy( str3, str1>
; cout << "strcpy( str3, str1>
: " << str3 << endl; // concatenates str1 and str2 strcat( str1, str2>
; cout << "strcat( str1, str2>
: " << str1 << endl; // total lenghth of str1 after concatenation len = strlen(str1>
; cout << "strlen(str1>
: " << len << endl; return 0; }

 

 

 

 


C++ meaning "C plus plus," is a programming language that was built off the C language.
C++ Programming Examples · Add two Numbers · Check Leap Year or Not · Make Simple Calculator · Interchange Digits of Number ·
Borland Turbo C is a discontinued integrated development environment (IDE>
and compiler for the C programming language from Borland.
C++ 11 is the second major version of C++ and the most important update since C++98.
The abbreviation of fabs c++ is the floating Absolute, which means the floating-point numeric values.
geeks for geeks c++ is a general-purpose programming language and is widely used nowadays for competitive programming.