DEV Community

Cover image for Part 8: Strings and Structs
Ayesha Sahar
Ayesha Sahar

Posted on • Edited on • Originally published at ayeshasahar.hashnode.dev

Part 8: Strings and Structs

Hey folks, welcome to the 8th part of my series, C++: Explain like I’m five. In the last part, we discussed arrays and multidimensional arrays. Since you guys understand arrays, we can move on to strings and structs ;)

Strings

They are words that are made up of characters, therefore they are known as sequences of characters. Basically, a string is a collection of two or more characters enclosed in double quotes. Strings in C++ are terminated by a null character, '\0'. There are two ways to create and use strings:

  1. By creating char arrays and treating them as a string
  2. By creating a string object

Let's discuss these two methods first.

1. Array of Characters

First, take a look at this example.

Example:

#include <iostream>
using namespace std;
int main(){
   char book[50];
   cout<<"Enter your favorite book name:";
   //reading user input
   cin>>book;
   cout<<"You entered: "<<book;
   return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Enter your favorite book name: To kill a Mockingbird
You entered: The
Enter fullscreen mode Exit fullscreen mode

Using cin for user input is inefficient as only the first word of the string is stored in char array and the rest get ignored. The cin function considers the space in the string as a delimiter and then ignores the rest.
You can see that only the “To” got captured in the book and the remaining part after space was ignored.

To deal with this situation, we can use cin.get function, which reads the complete line entered by the user.

Example:

#include <iostream>
using namespace std;
int main(){
   char book[50];
   cout<<"Enter your favorite book name:";
   //reading user input
   cin.get>>book;
   cout<<"You entered: "<<book;
   return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Enter your favorite book name: To kill a Mockingbird
You entered: To kill a Mockingbird
Enter fullscreen mode Exit fullscreen mode

See? Now the book array captured all of the user input.

Disadvantage of this method:

In this method, size of char array is pre-defined, which means that the size of the string created by this method, is fixed. During runtime, we cannot allocate more memory. For example, let's say that you created an array of characters with the size 20. The user enters the string of size 30. In this case, the last ten characters would be truncated from the string.
On the other hand, if you create a larger array to accommodate user input then the memory would be wasted if the user input is small and the array is much larger than what is needed.

String object in C++

To use this method, you should include an additional header file in the source code, the library.

Example:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string greeting = "Hello World!";
  cout << greeting;
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

String Concatenation:

The + operator can be used between strings to add them together in order to make a new string. This process is called string concatenation.

Example:

#include <iostream>
#include <string>
using namespace std;

int main () {
  string firstName = "Thomas ";
  string lastName = "Shelby";
  string fullName = firstName + lastName;
  cout << fullName;
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Thomas Shelby
Enter fullscreen mode Exit fullscreen mode

String length:

We can use the length() function to get the length of a string.

Example:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  cout << "The length of the string: " << abc.length();
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

The length of the string: 26
Enter fullscreen mode Exit fullscreen mode

Structures

It is a compound data type and has different variables of different types. For example, you want to store Employee details like employee name, employee age or employee salary. You can do it in two different ways. You can create different variables for each data, but the issue would be that if you want to store the details of multiple employees, in that case, it is not feasible to create a separate set of variables for each employee.

The second and the best way of achieving the same aim is by creating a structure like this:

struct Employee
{
    char name[30];
    int age;
    int salary;
};
Enter fullscreen mode Exit fullscreen mode

If you want to hold the information of two employees using this structure then you can do it like this:

Employee e1, e2;
Enter fullscreen mode Exit fullscreen mode

Example:

#include <iostream>
using namespace std;

struct Employee
{
    char name[60];
    char designation[60];
    int age;
    float salary;
};

int main()
{

    Employee e1;

    cout << "Enter Full Name: ";
    cin.getline(e1.name,60);
    cout << "Enter Designation: ";
    cin.getline(e1.designation,60);
    cout << "Enter Age: ";
    cin>>e1.age;
    cout << "Enter Salary(in Rs): ";
    cin>>e1.salary;

    cout << "\nDisplaying Information..............." << endl;

    cout << "\nName: " << e1.name << endl;
    cout << "Designation: " << e1.designation << endl;
    cout <<"Age: " << e1.age << endl;
    cout <<"Salary(in Rs): " << e1.salary << endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Enter Full Name: Sara Ali
Enter Designation: Manager
Enter Age: 28
Enter Salary(in Rs): 50000

Displaying Information...............

Name: Sara Ali
Designation: Manager
Age: 28
Salary(in Rs): 50000
Enter fullscreen mode Exit fullscreen mode

That's all guys. Hope you understood these concepts. The next article would be the last of this series and will be about file handling 😆 So, stay tuned :)

Note: To see the examples of some basic C++ programs, take a look at my github repo here

Let's connect!

Twitter

Github

Top comments (0)