Thursday, April 25, 2024 12:50

Table of contents >> Introduction > String variable type

String variable type

If you remember the lesson about char variable type, you know that a char can only store a single character. And you also remember that for storing more than a single character, I said you will be using the string variable type.

A string is basically a series of one or more characters. It’s default value is nullUnlike characters, which enclose their value between single quotation marks, strings enclose them in double quotation marks.

Lets see an example about declaring and initializing a string variable type:

The output of the above code will be this:

string variable type

Various text-processing operations can be performed using strings: concatenation (joining one string with another), splitting by a given separator, searching, replacement of characters and others. I will be explaining all them in a later part of our lessons.

The concepts explained in this lesson are also shown visually as part of the following video:

EXERCISES
1. Declare two variables of type string with values “Hello” and “World”. Declare another variable of type string and assign to it the value obtained from concatenation of the two string variables (add space if necessary). Print the third string variable on console.

Solution

2. A company dealing with marketing wants to keep a data record of its employees. Each record should have the following characteristic – first name, last name, age, gender (‘m’ or ‘f’), and unique employee number (27560000 to 27569999). Declare appropriate variables needed to maintain the information for an employee by using the appropriate data types and attribute names.

Solution


For the names use type string, for the gender use type char (only one char m/f), and for the unique number and age use some integer type.

Tags: , ,

Leave a Reply



Follow the white rabbit