Sie sind auf Seite 1von 3

Lab 8

Fundamentals of Programming

Lab Journal - Lab # 8


Name: _________________________________

Enrollment #: _________________________________

Class: _________________________________

Objective

This lab will cover char arrays and strings in C++.

Exercise 1

Use <cstring> library and run the following code lines in Visual studio.

Note: If you have a latest version of your compiler use _s with all functions e.g strcpy_s(). This
is the newer version of this function. The old one was strcpy().

Write the output and write which operation is being performed in each code.

1. Output:
char str1[20] = "Computer Science";
char str3[20];

strcpy(str3, str1);

cout << "str3 : " << str3 << endl;


cout << "str1 : " << str1 << endl;

2.
char str1[20] = "Computer Science";
char str2[20] = " World";

int length1, length2;

length1 = strlen(str1);
length2 = strlen(str2);
cout << "str1 length : " << length1 <<
endl;
cout << "str2 length : " << length2 <<
endl;

Computer Programming Lab


Page 1
Lab 8

3.
char str1[20] = "Fabulous";
char str2[20] = "Better";
char str3[20] = "bat";

cout << "str compare str1, str2 : " <<


strcmp(str1,str2) << endl;
cout << "str compare str2, str3 : " <<
strcmp(str2, str3) << endl;

4
char nameArr[20];
cout << "Enter your name";
cin.getline(nameArr,20);

cout << "You entered : " << nameArr;

Now Use a string library and See the output of the following. Write the output and write what
operation is being performed in each code.

5.
string str1 = "Fabulous";
string str2 = "Better";
string str3 = "bat";

str3 = str1;
cout << "str 1: " << str1 << endl;
cout << "str 2: " << str2 << endl;
cout << "str 3: " << str3 << endl;

6.
string str1 = "Programming ";
string str2 = "World";
string str3;

str3 = str1 + str2;


cout << "str 1: " << str1 << endl;
cout << "str 2: " << str2 << endl;
cout << "str 3: " << str3 << endl;

7.
string str1 = "Programming ";
string str2 = "World";
string str3;

str3 = str1 + str2;


int length = str3.size();
cout << "str3 length: " << length <<
endl;

Computer Programming Lab


Page 2
Lab 8

8. string str1 = "Fabulous";


string str2 = "Better";
string str3 = "Bat";

//str1.compare() works like strcmp, 0


equal, 1 greater, -1 smaller
cout << "Comparing str1 and str2 : " <<
str1.compare(str2)<<endl;
//== works differently. it returns 0 or
1. 0 when not same, 1 when same strings
cout << "Comparing str2 and str3 : " <<
(str2 == str3);
9.
string nameStr;
cout << "Enter your name";
getline(cin,nameStr);

cout << "You entered : " << nameStr;

Exercise 2

Write a program that inputs the names of five countries. It only display the
countries whose name starts with vowels.

Exercise 3

Write a C++ program that prompts the user to enter a word in a character array and
then checks whether it is a palindrome or not. (*NOTE: A palindrome is a word
which if reversed, spells the same e.g. RADAR if reversed spells RADAR so it is a
palindrome whereas HELLO when reversed is OLLEH which is not a palindrome.)

Exercise 4

Write a program that inputs a string and display the number of character in it.

Exercise 5

Write a program that inputs two strings from user and compare them using
strncmp function it also inputs the number of characters to be compared. The
program display whether the first string is less then, equal to or greater than
second string.

Computer Programming Lab


Page 3

Das könnte Ihnen auch gefallen