Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

C++ Strings

String is a collection of characters. There are two types of strings commonly used in C++
programming language:
1. Strings that are objects of string class (The Standard C++ Library string class)
2. C-strings (C-style Strings)
C-strings
In C programming, the collection of characters is stored in the form of arrays, this is also
supported in C++ programming. Hence it's called C-strings.
C-strings are arrays of type char terminated with null character, that is, \0 (ASCII value of
null character is 0).

How to define a C-string?


char str[] = "C++";
In the above code, str is a string and it holds 4 characters.

Although, "C++" has 3 character, the null character \0 is added to the end of the string
automatically.
Alternative ways of defining a string
char str[4] = "C++";
char str[] = {'C','+','+','\0'};
char str[4] = {'C','+','+','\0'};
Like arrays, it is not necessary to use all the space allocated for the string. For example:
char str[100] = "C++";

The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing
the string is one more than the number of characters in the word "Hello."

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++ −

Actually, you do not place the null character at the end of a string constant. The C++
compiler automatically places the '\0' at the end of the string when it initializes the array.

Example 1: C++ String to read a word


C++ program to display a string entered by user.

#include <iostream.h>
int main()
{
char str[100];

cout << "Enter a string: ";


cin >> str;
cout << "You entered: " << str << endl;

cout << "\nEnter another string: ";


cin >> str;
cout << "You entered: "<<str<<endl;

return 0;
}
Output
Enter a string: C++
You entered: C++

Enter another string: Programming is fun.


You entered: Programming

Notice that, in the second example only "Programming" is displayed instead of


"Programming is fun". This is because the extraction operator >> works as scanf() in C and
considers a space " " has a terminating character.

Example 2: C++ String to read a line of text


C++ program to read and display an entire line entered by user.

#include <iostream.h>
int main()
{
char str[100];
cout << "Enter a string: ";
cin.get(str, 100);

cout << "You entered: " << str << endl;


return 0;
}

Output

Enter a string: Programming is fun.


You entered: Programming is fun.
To read the text containing blank space, cin.get function can be used. This function takes
two arguments. First argument is the name of the string (address of first element of string)
and second argument is the maximum size of the array.

In the above program, str is the name of the string and 100 is the maximum size of the
array.

string Object
In C++, you can also create a string object for holding strings. Unlike using char arrays, string
objects has no fixed length, and can be extended as per your requirement.

Example 3: C++ string using string data type

#include <iostream.h>
int main()
{
// Declaring a string object
string str;
cout << "Enter a string: ";
getline(cin, str);

cout << "You entered: " << str << endl;


return 0;
}

Output

Enter a string: Programming is fun.


You entered: Programming is fun.

In this program, a string str is declared. Then the string is asked from the user. Instead of
using cin>> or cin.get() function, you can get the entered line of text using getline(). getline()
function takes the input stream as the first parameter which is cin and str as the location of
the line to be stored.

Passing String to a Function


Strings are passed to a function in a similar way arrays are passed to a function.

#include <iostream.h>
void display(char s[]);
int main()
{
char str[100];
string str1;

cout << "Enter a string: ";


cin.get(str, 100);
cout << "Enter another string: ";
getline(cin, str1);

display(str);
display(str1);

return 0;
}

void display(char s[])


{
cout << "You entered char array: " << s <<;
}

void display(string s)
{
cout << "You entered string: " << s << endl;
}

Output

Enter a string: Programming is fun.


Enter another string: Programming is fun.
You entered char array: Programming is fun.
You entered string: Programming is fun.

In the above program, two strings are asked to enter. These are stored in str and str1
respectively, where str is a char array and str1 is a string object. Then, we have two
functions display() that outputs the string onto the string.

The only difference between the two functions is the parameter. The first display() function
takes char array as a parameter, while the second takes string as a parameter. This process
is known as function overloading.

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


Sr.No Function & Purpose

1 strcpy(s1, s2);
Copies string s2 into string s1.

2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.

3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

strrev (s1);
5 Reverses the given string

strlwr(s1);
6 Converts string to lowercase

strupr(s1);
7 Converts string to uppercase

strcpy(to_string, from_string) — String Copy:


The strcpy() function copies the string pointed by from_string (including the null character)
to the character array to_string. This function returns character array to_string.

#include <iostream.h>
#include<string.h>
int main()
{
char a[10];
strcpy(a, “hello”);
cout << a;
return(0);
}

strlen(string) — String Length


strlen(str) returns the length of the string pointed to by str, i.e., the number of characters
excluding the null terminator.

#include <iostream.h>
#include<string.h>
int main()
{
char str[80];
cout << “Enter a string: “;
gets(str);
cout << “Length is: “ << strlen(str);
return(0);
}

strcat(string_1, string_2) — Concatenation of Strings


The strcat() function appends s2 to the end of s1. String s2 is unchanged.

#include <iostream.h>
#include<string.h>
int main()
{
char s1[21],
s2[11];
strcpy(s1, “hello”);
strcpy(s2, “ there”);
strcat(s1, s2);
cout << s1 << endl;
cout << s2 << endl;
return(0);
}

strcmp(string_1, string_2) — Comparison of Strings


The strcmp(str_1, str_2) function compares two character by character. If the first character
of two strings is equal, next character of two strings is compared. This continues until the
corresponding characters of two strings are different or a null character '\0' is reached.

Return Value from strcmp()

Return Value Remarks

0 if both strings are identical (equal)

negative if the ASCII value of first unmatched character is less than second.

positive integer if the ASCII value of first unmatched character is greater than second.

The strings are compared lexicographically (i.e., according to dictionary order): a < aa < aaa
< … < b < ba < bb < … < bz < baa < … < abca < abd < ...

#include <iostream.h>
#include<string.h>
int main()
{
char str[80];
cout << “Enter password: “;
gets(str);
if(strcmp(str, “password”))
{ // strings differ
cout << “Invalid password.\n”;
}
else
cout << “Logged on.\n”;
return(0);
}
Strrev(string)- reverses a given string

strrev(string ) function reverses a given string in C language. Syntax for strrev( ) function is given
below.

char *strrev(char *string);

#include <iostream.h>
#include<string.h>

int main()
{
char name[30] = "Hello";
cout <<"String before strrev( ) : "<<name;
cout <<"String after strrev( ) : "<<strrev(name);
return 0;
}

strlwr(s1)- Converts string to lowercase


strlwr( ) function converts a given string into lowercase. Syntax for strlwr( ) function is given below.

char *strlwr(char *string);

#include <iostream.h>
#include<string.h>
int main()
{
char str[ ] = "MODIFY This String To LOwer";
cout <<strlwr (str);
return 0;
}

strupr(s1) - Converts string to uppercase


strupr( ) function converts a given string into uppercase. Syntax for strupr( ) function is given below.

char *strupr(char *string);

#include <iostream.h>
#include<string.h>
int main()
{
char str[ ] = "MODIFY This String To Upper";
cout <<strupr (str);
return 0;
}

You might also like