C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Strings

C++ Strings

shape Introduction

As discussed in previous chapters, there is no string data type in C++ language. Hence, the group of characters without data type forming a word, can be given individually as an array of characters using Strings. There are two types of CPP Strings and they are:

String

shape Description

A String can be defined as an array of characters that are terminated with null character \0. A compiler assigns the null character if the developer does not specify. To read strings given by user, %s is used in scanf function. CPP Strings takes the help of 'char'.

Declaration of String in C

String declaration will be similar to array declaration. But here, CPP Strings are of char type. Eg:
char i[5];

Initialization of String in C

C strings can be initialized in various ways. Eg:
char i[]="xyz"; OR char i[5]="xyz"; OR char i[]={'x','y','z','\0'}; OR char i[4]={'x','y','z','\0'};

Strings using arrays

shape Description

They are used when one knows how many characters are given in the array. The process of assigning characters during compile time is known as "Static Allocation".

shape Conceptual figure

shape Syntax

char String_variable_name[size];
Eg:
char c[]={'s', 'p','l','e','s','s','o','n','s','\0'}; or char c[10]="splessons";

Disadvantages using C- Strings

shape Description

In C++-strings, the characters are read only till the separator. If the compiler detects the separator, it outputs only till the string and the characters after the separator are ignored.

shape Example

[c] #include<iostream> using namespace std; int main() { char s[30]; cout << "Enter a string: "; cin >> s; cout << "The string entered is : "<< s <<endl; cout << "\nEnter another string: "; cin >> s; cout << "You entered: "<< s <<endl; return 0; }[/c] Output: [c]Enter a string: splessons The string entered is : splessons Enter another string: welcome to splessons You entered: welcome [/c] As seen in the above example, when 'spleesons' is entered, the compiler reads it as single string without any separators like "Spaces, Tabs or New Lines". But, when 'welcome to splessons' is entered due to spaces between characters only first string is displayed and the rest of characters are omitted. To avoid omission of the characters, C++ strings are used as objects of string class.

Strings as objects of string class

shape Description

The above example of reading the string can be done using C++. Here, C++ provides the facility of reading the entire string using getline() function.

shape Example

[c] #include<iostream> #include<string> using namespace std; int main(void) { string str1,str2; cout<< " Enter a string "<< endl; getline(cin,str1); cout<<"The string entered is: "<< str1 << endl; cout<<"Enter another string: "<<endl; cin >> str2; cout<< "The string entered is " << str2 << endl; return 0; }[/c] Output: [c] Enter a string welcome to splessons The string entered is: welcome to splessons Enter another string: welcome to splessons The string entered is welcome [/c] Some of the string functions are given in the below table:

shape C++ Strings

String Functions Description
strrev() Reverses the given string
strset() Sets all character in a string to given character
strnset() Sets the portion of characters in a string to given character
strtok() Tokenizing given string using delimiter
strncpy() Copies given number of characters from one string to another
strlen() Gives the length of string
strdup() Duplicates the string
strlwr() Converts the string to lower case
strupr() Converts the string to upper case
strcat() Concatenates the two string values
strcmp() Compares the two strings
strcmpi() Compares the two strings but it is not case sensitive
strchr() Pointer points to first occurrence of a character in string
strrchr() Pointer points to last occurrence of a character in string
strstr() Pointer points to first occurrence of the second string
strrstr() Pointer points to last character of second string

Concatenation of strings

shape Definition

Concatenation brings the strings together and is done with + operator. Concatenation can also be done using standard library function strcat().

shape Example

Example for concatenation, copying and finding length of strings without using strcat() function. [c] #include<cstring> #include<iostream> using namespace std; int main () { string s1 = "Hi,you are reading"; string s2 = " splessons"; string s3; int length ; // copy string1 into string3 s3 = s1; // concatenates string1 and string2 s3 = s1 + s2; cout << "s1 + s2 : " << s3 << endl; // lenghth of string3 after concatenation length = s3.size(); cout << "s3.size() : " << length << endl; return 0; }[/c] Output [c] s1 + s2 : Hi,you are reading splessons s3.size() : 28 [/c]

shape Example

Below is an example to find number of similar characters in a string. [c] #include <cstring> #include <iostream> using namespace std; int main() { char c[100],characters; int i,count=0; cout << "Enter a string: "; cin.getline(c,100); cout << "Enter a character to find how many times it is repeated : "; cin >> characters; for(i=0; c[i]!='\0'; ++i) { if(characters==c[i]) ++count; } cout << "number of times " << characters << " is repeated = " << count; return 0; }[/c] Output: [c] Enter a string: hello you are reading splessons Enter a character to find how many times it is repeated : o number of times o is repeated = 3[/c] This search process can also be done using find() function

Summary

shape Key Points

  • CPP Strings are group of characters.
  • In initialization of the string, if the specific number of character are not initialized then rest of characters are initialized with NULL(/0).

shape Programming Tips

  • Strings are always enclosed in double quotes " ".
  • While declaration of string, size must be mentioned otherwise compiler gives an error.