Split First Name and Last Name using C++

Splitting your first name and last name from your full name is one of the real-time problems that you can get as a question in any coding interview. It is not a difficult question to solve at all. So if you want to learn how to split your first name and last name from your full name by using the C++ programming language, then this article is for you. In this article, I will take you through a tutorial on how to split your first name and last name using C++.

Split First Name and Last Name using C++

There are many ways to split your first name and last name from your full name, which also depends on the programming language you are using, as different programming languages use different methods to split a piece of text into parts. For example, in Python, we have an inbuilt function split() to split a piece of text, just like that, in C++, we have an inbuilt function getline() which reads characters from user input and stores them in the string variable until a new line occurs. In simple words, it extracts strings from an input line by line. We can use this function to split first name and last name from a full name.

So below is how you can split first name and last name from your full name using C++:

Enter Your Full Name: Aman Kharwal
Your First Name: Aman  
Your Last Name: Kharwal

In the above code, I have used the cin.get() method after taking a user input which is being used to find a line between two words. And then, the getline() function stores the first name into cin and the last name into the “last” variable.

Also, Read – C++ Programs for Beginners with Code.

Summary

So this is how you can separate two words from a piece of text using the C++ programming language. Splitting your first and last name from your full name is one of the real-time problems you can get as a question in any coding interview. Hope you liked this article on how to split first and last name from full name using C++. Please feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1126

Leave a Reply