in , ,

C++ Strings

C++ Strings
C++ Strings

C++ Strings: In this article, you’ll learn to handle strings in C. You’ll learn to declare them, instate them, and use them for different input/output tasks.

The string is an assortment of characters. There are two sorts of strings generally used in C++ programming language:

• Strings that are objects of string class (The Standard C++ Library string class)

• C-strings (C-style Strings)


C-strings

In C programming, the assortment of characters is stored in the form of arrays, this is additionally supported in C++ programming. Henceforth it’s called C-strings.

C-strings are arrays of type char ended with invalid character, that is, \0 (ASCII estimation of the 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++";

Example 1: C++ String to read a word

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

#include <iostream>
using namespace std;

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 shown as opposed to “Writing computer programs is entertaining”.

This is on the grounds that the extraction operator >> functions like scanf() in C and thinks about a space ” has an ending character.


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

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

#include <iostream>
using namespace std;

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 peruse the content containing clear space, cin.get capacity can be utilized. This capacity takes two contentions.

The first contention is the name of the string (address of the first component of string) and the second contention is the most extreme size of the array.

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


string Object

In C++, you can likewise make a string object for holding strings.

Unlike to using char arrays, string objects has no fixed length, and can be reached out according to your prerequisite.


Example 3: C++ string using string data type

#include <iostream>
using namespace std;

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 array is passed to a function.

#include <iostream>

using namespace std;

void display(char *);
void display(string);

int main()
{
    string str1;
    char str[100];

    cout << "Enter a string: ";
    getline(cin, str1);

    cout << "Enter another string: ";
    cin.get(str, 100, '\n');

    display(str1);
    display(str);
    return 0;
}

void display(char s[])
{
    cout << "Entered char array is: " << s << endl;
}

void display(string s)
{
    cout << "Entered string is: " << s << endl;
}

Output

Enter a string:  Programming is fun.
Enter another string:  Really?
Entered string is: Programming is fun.
Entered char array is: Really?

In the above program, two strings are approached to enter. These are put away in str and str1 separately, where str is a char array and str1 is a string object.

At that point, we have two functions display() that yields the string onto the string.

The main contrast between the two capacities is the parameter. The main presentation() work takes a char array as a parameter, while the subsequent takes a string as a parameter.

This process is known as function overloading. Learn more about Function Overloading.


Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

TopStore App

TopStore App – 3rd Party App Downloader for iPhone

ACMarket

ACMarket AppStore – Exclusive Android App Store for Apps and Games