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

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_