Home Classroom C++ C++ with Barney! Class 10

C++ with Barney! Class 10

1267

Characters and their arrays

[You're Ted and you decide to learn some C++ from your friend Barney. This is the tenth day of the class]

You: Hi, Barney!

Barney: Hi, Ted! I was waiting for you.

You: I was a bit late, had some homework to do.

Barney: Oh, I know the kind of homework you do, he he. Anyway, where did we stop last time?

You: We discussed about arrays for two days.

Barney: Ah, yes. Then, enough of arrays. Now that you know how arrays operate, let’s talk about characters and their arrays, or strings, as we usually call them.

You: Okay.

Barney: So you know what a character is?

You: I think I do. It’s a single letter like ‘a’ or ‘b’, right?

Barney: Yes, not only letters, digits, punctuation marks like dots, commas, and special symbols, all of these belong to the set of characters. In fact the ASCII system has 256 characters in it.

You: Yeah, I’ve heard of that.

Barney: Good. We use the ‘char’ data type to identify characters in C++. Let’s see how they work from an example. Suppose you need to write a program where you need to do something and then prompt the user to ask whether he needs to do that again. If the user says yes, the process will be repeated. Let’s write a program that gives the two times of a given input.

You: He he, that’s a nice thing to program.

Barney: Just for the sake of exploring the use of ‘char’. First we ask the user to input an integer and we double it and print it in the screen. It’s as simple as this:

cout<<“Enter a number: “;
cin>>a;
count<<“Two times”<<a<<” is “<<(a*2)<<“\n”;

You: Okay.

Barney: Right, now the problem is how to ask the user to enter a value. We can use ‘cin’ for this. Also we can check his input using an ‘if’ condition. But we have to repeat the above code after that. How can we achieve that?

You: By using a loop, perhaps?

Barney: Exactly! We can use a while loop to do that. Let’s see how.

do{
cout<<“Do you want to continue [Y/N]? “;
cin>>c;
} while (c==‘y’ || c==‘Y’);

You: I see, it’s not that difficult. That ‘c’ should be declared as a ‘char’, right?

Barney: Of course. Let’s now complete the program:

#include<iostream>

using namespace std;

int main()
{

int a;
char c;
do{
cout<<“Enter a number: “;
cin>>a;
cout<<“Two times “<<a<<” is “<<(a*2)<<“\n”;

cout<<“Do you want to continue [Y/N]? “;
cin>>c;
} while (c==‘y’ || c==‘Y’);

return 0;

}

And let’s see if it works:

You: Hmm, works as expected.

Barney: Yes. That’s about chars. Now let’s talk about sequences of characters. We can define them using character arrays like this:

char name[]={‘d’,‘i’,‘g’,‘i’,‘t’,‘\0′};

You: But what’s that ‘\0’ thing at the end?

Barney: It’s called the null character. It signals that the character sequence ends there. Otherwise compiler doesn’t know where to stop. You’ll understand it more when we deal with pointers.

You: Okay.

Barney: For now, keep in mind that you have to add the null terminator when you declare character arrays like that. After assigning such an array we can print the whole word like this:

cout<<name;

You: I see. So you need not print character by character?

Barney: Of course not. But declaring strings in the above way is hard work. We can simplify this by using double quotes like this:

char name[]=“digit”;This one does the same job as our previous statement.

You: But what about the null character?

Barney: When you put double quotes, the null character is automatically appended. We call this adding the null character ‘implicitly’. In the former method we put it ‘explicitly’.

You: Okay.

Barney: Let’s see how to use such a character array in a program. Let’s write a program which asks the user to input his name and prints it back and mention the second letter in his name:

#include<iostream>

using namespace std;

int main()
{

char name[50];

cout<<“Enter your name: “;
cin>>name;
cout<<“Your name is “<<name<<” and the second letter in your name is”<<name[1]<<“\n”;

system(“pause”);

return 0;

}

You: But why name[50] instead of name[] ?

Barney: As in usual arrays, name[50] tells that the maximum number of characters we store in ‘name’ is 50. Of course it can be lesser than that, but not more.

You: I see. Shall we run it and see?

Barney: Go ahead.

Barney: He he, you know that name is banned in here these days.

You: Yeah, yeah, it’s the trending topic these days ne ;)

Barney: Alright then, that’s a brief introduction to character arrays. The next day we’ll talk about strings, and the string library functions C++ provides for us.

You: Okay, thanks. Bye Barney!

Barney: Bye, Ted!

Comments

comments

Engineer. Loves tic-tac.

NO COMMENTS

Leave a Reply