1 Crore+ students have signed up on EduRev. Have you? |
A string of characters is stored in successive elements of a character array are terminated by the NULL character.
How many maximum number of parameters does a string constructor can take?
string(other_string, position, count). It is a type of constructor for the string.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
string str2="Steve jobs";
string str3="He founded apple";
str.append(str2);
str.append(str3, 6, 3);
str.append(str3.begin() + 6, str3.end());
str.append(5,0x2e);
cout << str << '\n';
return 0;
}
In this program, We are adding characters to the string, So the output will as follows after addition of characters.
Output:
$ g++ str.cpp
$ a.out
Steve jobsndended apple.....
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("I like to code in C");
unsigned sz = str.size();
str.resize (sz + 2, '+');
str.resize (14);
cout << str << '\n';
return 0;
}
In this program, We are resizing the string by adding + and then we are resizing it to 14.
Output:
$ g++ basicst.cpp
$ a.out
I like to code
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name ("Jobs");
string family ("Steve");
name += " Apple ";
name += family;
name += '\n';
cout << name;
return 0;
}
In this program, We are adding the characters at the end of the current value.
Output:
$ g++ str1.cpp
$ a.out
Jobs Apple Steve
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs founded the apple");
string str2 ("apple");
unsigned found = str.find(str2);
if (found != string :: npos)
cout << found << '\n';
return 0;
}
In this program, We are finding a string by using the find method.
$ g++ basicst2.cpp
$ a.out
23
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs");
cout << str.length();
return 0;
}
In this program, We are finding the length of the string.
Output:
$ g++ str3.cpp
$ a.out
10
What will be the output of the following C++ code?
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs");
char * cstr = new char [str.length() + 1];
strcpy (cstr, str.c_str());
char * p = strtok (cstr," ");
while (p != 0)
{
cout << p << '\n';
p = strtok(NULL," ");
}
delete[] cstr;
return 0;
}
In this program, We are breaking up the strings into the form of tokens.
Output:
$ g++ basicst4.cpp
$ a.out
Steve
jobs
An empty string is a character array with the NULL character in the zeroth index position.
There can be one or two parameters in resize method. They are string length and an optional new character to be inserted.
The string class is an instantiation of the basic_string class template.
To use the string class, We have to use #include<string> header file.
The characters of a string are stored contiguously in the memory.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Test string");
for ( string :: iterator it = str.begin(); it != 5; ++it)
cout << *it;
return 0;
}
In the for loop, We are not allowed to give a numeric value in string, So it is arising an error.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs");
cout << str.capacity() << "\n";
return 0;
}
str.capacity() returns the size of the storage space currently allocated for the string, expressed in terms of bytes and capacity of the string may be equal or greater. So the answer is not fix, depends on the compiler.
Output:
$ g++ basicst1.cpp
$ a.out
10
15 videos|20 docs|13 tests
|
Use Code STAYHOME200 and get INR 200 additional OFF
|
Use Coupon Code |
15 videos|20 docs|13 tests
|
|
|
|
|
|
|
|