43: ***(Syntax)*** Some useful ---string--- methods

  • .length( )
.length( ) function tells you the length of the string.

#include <iostream>
using namespace std;
int main()
{
    int charcters;
    string word;

    cout << "Enter a word: ";
    getline(cin, word);

    cout << "There are " << word.length() << " characters in word: " << word;

    return 0;
}
  • .empty( )

.empty( ) function is a boolean attribute that is used when you have to check whether the user have given some input or not

#include <iostream>
using namespace std;
int main(){

    string name;

    cout << "Enter your name: ";
    getline(cin, name);

    if(name.empty()){
        cout << "You didn't write anything \n";
    }
    else cout << "Welcome " << name;

    return 0;
}

In this program, if no input is given by the user, You didn't write anything will be the output. Otherwise, Welcome name will be the output.

  • .clear( )

.clear( ) is used to clear everything from the variable with which it is written.

#include <iostream>
using namespace std;
int main()
{
    string name;
    cout << "Enter your name: ";
    getline(cin, name);

    name.clear();

    cout << "Your name is: " << name;
   
    return 0;
}

Now, in this program, when we used .clear( ) function, everything within name string was deleted and the output will be: Your name is: only

  • .append( )

.append( ) is used to add some predefined text in the string. To add the text you have to add that text between ( ) of .append( ) 

For example, You want to add @gmail.com after the name entered by the user:

#include <iostream>
using namespace std;
int main()
{
    string name;
    cout << "Enter your name: ";
    getline(cin, name);

    name.append("@gmail.com");

    cout << "Your username is: " << name;

    return 0;
}

whatever the name is written by the user, in output, there will be @gmail.com

  • .at( )

.at( ) is used to check which character is on the certain position number in a string.

    #include <iostream>
    using namespace std;
    int main()
    {
        string name;
        int num=0;

        cout << "Enter your name: ";
        getline(cin, name);

        cout << "Enter the number of the character, that you want to check in string: ";
        cin >> num;

        cout << name.at(num);

        return 0;
    }

Note. counting starts from 0

  • .insert( )

.insert( ) is used to insert a character in a string

To do this in ( ) first write index(serial of character before which you have to insert character. This starts from 0 ie first character is at position 0) then add a comma then add what you want to insert within " "

    #include <iostream>
    using namespace std;
    int main()
    {
        string name;
        int num=0;

        cout << "Enter your name: ";
        getline(cin, name);

        name.insert(0, "@");

        cout << name;

        return 0;
    }


  • .find(' ')

This is used to find first white space in the string.
remember index number starts from 0.

    #include <iostream>
    using namespace std;
    int main()
    {
        string name;
        int num=0;

        cout << "Enter your name: ";
        getline(cin, name);

        cout << name.find(' ');

        return 0;
    }

  • .erase( )

This is used to remove specific portion of a string.
In ( ) write starting index, then comma, then ending index. 







Comments

Popular posts from this blog

153: Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes. Note: The possible banknotes are 500, 100, 50, 20, 10, 5, 2, and 1

206: Write a program to create a class named "Circle" which has the property "radius". Define functions to calculate the area and circumference of the circle.

221: // In Task 2, we discussed multilevel inheritance with parameterized constructors for Student, UndergraduateStudent, and GraduateStudent classes in a university management system. Can you explain the advantages of using multilevel inheritance with specific details about the functions and data members in these classes? How were the parameterized constructors (e.g., setting student name, age, and ID) used to ensure that each class in the hierarchy correctly initializes its properties, such as creating an UndergraduateStudent named "John," aged 20, with a student ID of 12345