C++ Strings - Access - Namespace

In C++ Programming, the data type variable Strings has a big part like C++ String Access Namespace, it means

  • How you can get access to your strings later I explain, now, the second
  • How to give users input the string variable to the program in c++.
  • The third is using Namespace to use better strings functions.

Access String

In C++ programming, you will be able to access string character with this method, You can get every character on strings with an index number and the syntax is entered the index number in box brackets []. All data are also available on FB.

So, remember, In c++, we start index number from zero 0.

The first example is using to get first letter of string:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string myname = "TCMaster";
	cout << myname[0];
        return 0;
}

Then, the output will be:

T

So, The second example will tell you that how you can change the characters in strings. refer to the index number and use single quotes.

Example:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string myname = "Hellow";
        myname[0] = 'Y';
	cout << myname;
        return 0;
}

So, the Output:

Yellow

C++ Strings – Access – Namespace – User input

So, Now! students I am telling you how you can give user input in your programs. some people thought that they can give user input like characters char input or integer int.

This method is not valid for more then 1 String word.
string myName;
cout << “Enter your name “;
cin >> myName;
cout << “Your Name is: “; << myName;

So, complete example is here.

#include <iostream>
#include <string>
using namespace std;
int main(){
	string myName;
    cout << "Enter your name: ";
    cin >> myName;
    cout << "Your Name is: " << myName;
    return 0;
}

Then, the output:

Enter your name: Ali
Your Name is: Ali

So, Now! I explain a method in which you can get more than one string word, just like you enter “Ali Hamza” or enter “Sahil Sultan and Ali Hamze” will return all the string. Now see the Example.

So, the example is here:

#include <iostream>
#include <string>
using namespace std;
int main(){
	string fullName;
	cout << "Type your full name: ";
	getline (cin, fullName);
	cout << "Your name is: " << fullName;
	return 0;
}

Then, see output:

Type your full name: Sahil Sultan and Ali Hamze
Your name is: Sahil Sultan and Ali Hamze

Omitting Namespace

In this web, may-be you read the chapter of the string header file, but here again, I explain the Omitting Namespace of string, In strings function, we use std:: plus the string code lines like the example below.

So, the Example:

#include <iostream>
#include <string>

int main() {
  std::string greets = "Good-Day";
  std::cout << greets;
  return 0;
}

Then, the Output:

Good-Day

It depends on you if you want to include the standard namespace library or not, the choice is yours.

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

One thought on “C++ Strings – Access – Namespace – Learn With TCMaster”
  1. Wow wonderful way to teach C++
    I want to request you that make Youtube channel and upload c++ course relative videos.

    Thanks in Advance

Leave a Reply

Your email address will not be published. Required fields are marked *