Strings
,Strings in C++
• A String is called as group of characters enclosed in double quotes.
• String creations in C++
• C-style strings(The one-dimensional character array is used to store a string
• Using string class
• C-style string creation
• A string is defined as a character array terminated by a null symbol ( ‘\0’ ).
• Syntax:
char variablename[size];
• Example:
char s[20];
, #include < iostream>
C-style Strings in C++ int main()
{
• Example: char str[80];
cout << “Enter a string: “;
• Reading input using cin cin >> str;
• Problem: Entering the string “This is a cout << “Here is your string: “;
cout << str;
test”, the above program only returns return(0);
“This”, not the entire sentence. }
• Reason: The C++ input/output system #include <iostream>
stops reading a string when the first int main()
{
whitespace character is encountered. char str[80];
cout << “Enter a string: “;
• Solution: Use another C++ library
gets(str);
function, gets(). cout << “Here is your string: “;
cout << str << endl;
return(0);
}
,Strings in C++
• A String is called as group of characters enclosed in double quotes.
• String creations in C++
• C-style strings(The one-dimensional character array is used to store a string
• Using string class
• C-style string creation
• A string is defined as a character array terminated by a null symbol ( ‘\0’ ).
• Syntax:
char variablename[size];
• Example:
char s[20];
, #include < iostream>
C-style Strings in C++ int main()
{
• Example: char str[80];
cout << “Enter a string: “;
• Reading input using cin cin >> str;
• Problem: Entering the string “This is a cout << “Here is your string: “;
cout << str;
test”, the above program only returns return(0);
“This”, not the entire sentence. }
• Reason: The C++ input/output system #include <iostream>
stops reading a string when the first int main()
{
whitespace character is encountered. char str[80];
cout << “Enter a string: “;
• Solution: Use another C++ library
gets(str);
function, gets(). cout << “Here is your string: “;
cout << str << endl;
return(0);
}