Reading and Writing Strings in C++

Reading and Writing Strings in C++:

In this article, I am going to discuss Reading and Writing Strings in C++ Language with examples. Please read our previous article, where we gave a brief introduction to Strings in C++ with examples. Here, we will explain how to read a string from the keyboard. A string that is formed using character.

Reading and Writing Strings in C++:

In our previous article, we have explained that a string can be formed by using character as well as using class. In our upcoming articles, we will learn about the string class. Let us understand how to read a string from the keyboard with the help of the following piece of code.

char s[10];
cout << “Enter your name”;
cin >> s;

Here we have a character array s of size 10. So, this will be an array of characters and it can hold up to 10 characters. As we know very well that we need space for ‘\0’ which is a null character. So a total of 9 alphabets can be stored in this character array and 1 space for the null character.

Next, we are trying to read a name of a person or the name of a user. So here we have given a message “Enter your name”. It’s a good practice to give a message before taking input so that a user will know what you are asking for.

cin >> s will read a string from the keyboard and it will store in the array s. Suppose the user gave an input string as “Shane”, so it will store in the array as,

Reading and Writing Strings in C++

Then “Shane” will be stored like this and followed by that null character (\0) that will automatically be included. So, we don’t have to mention it.

All the built-in functions of C++ will follow this method that whenever we are dealing with the string it will include null character. So out of 10 spaces, only 6 spaces are used and the rest of the spaces are vacant. We can type any name from the keyboard and hit enter and all those alphabets will be stored followed by null character.

Example Reading and Writing Strings in C++:
#include <iostream>
using namespace std;
int main()
{
    char s[10];
    cout << "Enter your name:\n";
    cin >> s;
    cout << "Welcome " << s;
}
Output:

Example Reading and Writing Strings in C++

The problem in this Approach:

Now there is a problem with this method. The problem is, if you are writing a full name that is more than one word or if you’re typing a sentence then this will read only the first word. Suppose you want to enter the name as “Shane Roy”. Then you have to type “Shane” then space then “Roy”. After the space, this ‘cin>>s’ will treat “Roy” as another string. It will take only the first string that is “Shane”.

Example:
#include <iostream>
using namespace std;
int main()
{
    char s[10];
    cout << "Enter your name:\n";
    cin >> s;
    cout << "Welcome " << s;
}
Output:

Reading and Writing Strings in C++

Then how to handle this? So, to handle that we have another method of reading a string. For that consider the following code.

char s[30];
cout << “Enter your name”;
cin.get(s, 30);
cin.getline(s, 30);

We have taken a larger size array of size ‘30’, then here we ask for a ‘name’. Here, we have used one built-in function that is ‘get()’ as well as there is another built-in function ‘getline()’. So, we can use any of these functions.

Both the functions ‘get()’ and ‘getline()’ will take two parameters. The first parameter is the array where we want to store the input string and the second is the maximum number of characters that we are expecting from the keyboard. In this case, the max character is ‘30’. It will not take alphabets beyond 30. But we can enter less than 30 characters.

Now, if we type “Shane Roy”, then it will store both the words. If we add more words to this string then it will include them in a string but a maximum of 30. If you give more than 30 alphabets then it will take just 29 alphabets plus null character.

If you want to display the string then we will write ‘cout << s’. It will display the string without a null character. So, it will print all the alphabets until it reaches ‘\0’. Now let us see all these things in the C/C++ program.

Reading and Writing Strings in C++ Program:
#include <iostream>
using namespace std;
int main()
{
    char s1[30], s2[30];
    cout << "Enter your names:\n";
    cin.get (s1, 30);
    cin.ignore ();
    cin.get (s2, 30);
    cout << "Welcome " << s1 << " and " << s2;
}
Output:

Reading and Writing Strings in C++ Program

In the next article, I am going to discuss Built-in String Functions in C++ with examples. Here, in this article, I try to explain Reading and Writing Strings in C++ Language with examples. I hope you enjoy this Reading and Writing Strings in C++ with examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *