Strings


  1. What is the output of this program?
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    string str ("Prayagraj Shakya");
    cout << str.capacity() << "\n";
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    In this program, We are finding the capacity that the str can hold.


  1. What is the output of this program?
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    string s ("Bjarne Stroustrup founded the C++");
    string s2 ("C++");
    unsigned found = s.find(s2);
    if (found != string :: npos)
    cout << found << '\n';
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    In this program, We are finding a string by using the find method.



  1. What is the output of this program?
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    string s ("Ajit Kumar");
    unsigned found = s.find_first_of("Gupta");
    while (found != string :: npos)
    {
    s[found] = '*';
    found = s.find_first_of("Gupta", found + 1);
    }
    cout << s << '\n';
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    In this program, We are replacing the vowels with a asterisk by using find_first_of method.


  1. What is the output of this program?
     #include <iostream>  
    #include <cstring>
    #include <string>
    using namespace std;
    int main ()
    {
    string s ("Manjesh Ojha");
    char * cstr = new char [s.length() + 1];
    strcpy (cstr, s.c_str());
    char * p = strtok (cstr," ");
    while (p != 0)
    {
    cout << p << '\n';
    p = strtok(NULL," ");
    }
    delete[] cstr;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are breaking up the strings into the form of tokens.



  1. What is the difference between unsigned int length() and unsigned int size()?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Both of them will return the length of strings in same notations.