Course Content
Introduction to Arrays
Working with I/O Stream and Files in C++
0/2
Learn C++
About Lesson

C-Strings

There are two kinds of strings commonly used in C++:

  • C-strings and
  • strings that are objects of the string class.

In this section, we’ll describe the first kind. C-strings are arrays of type char. We call these strings C-strings or C-style strings

C-String Variables

Strings can be variables or constants. Here’s an example that defines a single-string variable.

It asks the user to enter a string and places this string in the string variable. Then it displays the string.

Example

#include <iostream>
using namespace std;
int main()
{
    const int MAX = 80; //max characters in string
    char str[MAX]; //string variable str
    cout <<"Enter a string: ";
    cin >> str; //put string in str
    //display string from str
    cout <<"You entered: " << str << endl;
    return 0;
}

Example: setw(size) manipulator

#include <iostream>
#include <iomanip> //for setw
using namespace std;
int main()
{
const int MAX = 20; //max characters in string
char str[MAX]; //string variable str
cout << "\nEnter a string: ";
cin >> setw(MAX) >> str; //put string in str,
// no more than MAX chars
cout << "You entered: " << str << endl;
return 0;
}

This program uses the setw manipulator to specify the maximum number of characters the input buffer can accept. The user may type more characters, but the >> operator won’t insert them into the array.

String Constants

You can initialize a string to a constant value when you define it.

#include <iostream>
using namespace std;
int main()
{
char str[] = "I am learning C++";
cout << str << endl;
return 0;
}

Reading Embedded Blanks

If you tried with strings that contained more than one word, you may have had an unpleasant surprise.

Example:

Enter a string: Law is a bottomless pit.
You entered: Law

It turns out that the extraction operator >> considers a space to be a terminating character.

Thus it will read strings consisting of a single word, but anything typed after a space is thrown away.

To read text containing blanks we use another function, cin.get().

// reads string with embedded blanks
#include <iostream>
using namespace std;
int main()
{
const int MAX = 80; //max characters in string
char str[MAX]; //string variable str
cout << "\nEnter a string: ";
cin.get(str, MAX); //put string in str
cout << "You entered: " << str << endl;
return 0;
}

Output

Enter a string: Welcome to C++.
You entered: Welcome to C++.

Reading Multiple Lines

  • cin::get() function can take a third argument to help out in this situation.
  • This argument specifies the character that tells the function to stop reading.
  • The default value for this argument is the newline (‘\n’) character, but if you call the function with some other character for this argument, the default will be overridden by the specified character.
#include <iostream>
using namespace std;
const int MAX = 2000; //max characters in string
char str[MAX]; //string variable str
int main()
{
cout << "\nEnter a string:\n";
cin.get(str, MAX, '$'); //terminate with $
cout <<"You entered:\n" << str << endl;
return 0;
}

Output

Enter a string:
C++ is a structured and object oriented 
programming language. It is 
developed by Bjarne Stroustrup.
It is easy to learn!!
$

You entered:
C++ is a structured and object oriented
programming language. It is
developed by Bjarne Stroustrup.
It is easy to learn!!
8
0
Join the conversation