Lab Manual 09
Lab Manual 09
Lab Manual 09
LAB MANUAL 09
STRINGS
Lab Objectives:
Strings
By now, you have learned how to declare a char variable to hold characters. ; You might also
have learned that a string (like a person's first or last name) in C++ can simply be represented by
a null terminated sequence of characters stored in a char array. These days you can use the string
type for strings. In the bad old days, before circa 1994, you had to use character arrays, because
there was no native string type in C++ most implementations. There is still no string type in C. A
string, represented as a character array, is called a "C string". Here's a quick little review of
declaring and initializing C strings.
char s1[8];
Actually, if you are assigning a value at the same time as you are declaring the char array, you
don't need to specify an array size.
Assigning a value either way results in a character array of finite length, established at compile
time.
Note: You cannot assign the value of a C string after the declaration as you do with other simple
variable assignments.
To assign a value after the declaration, you need to use a C String function such as strcpy. We'll
look at that next.
C++ supports a wide range of C string manipulation functions. Following are some of them:
1.1 strcpy
Use this function if you would like to copy one string to another string.
#include <string.h>
#include <iostream>
int main()
strcpy(Copy, Original);
return 0;
Before strcpy():
After strcpy():
1.2 strlen:
Use this function if you would like to know the length of a string.
#include <string.h>
#include <iostream>
int main()
cout << "Its length is: " << strlen(String) << " characters\n";
return 0;
String Class:
C String is one of the two ways that you can manipulate strings in C++. The other way is through
the string class. This allows strings to be represented as objects in C++. The string class gives
you strings of unbounded length because the string space is allocated at run-time. Compare this
to C Strings where the finite string length of char array variables is defined at compile time.
Moreover, assignment, comparison, and concatenation of strings using the string class is
arguably easier than using C Strings. However, you'll still see C String code around in older
programs so it's best to know both. Why, you may ask, do we need the string class when we
already have C string? Here is why: null terminated strings (C strings) cannot be manipulated by
any of the standard C++ operators. Nor can they take part in normal C++ expressions. For
example, consider this fragment:
s1 = "one"; // error
s2 = "two"; // error
s3 = "three"; //error
As the comments show, in C++, it not possible to use the assignment operator to give a character
array a new value. To do this, you need to use the strcpy function that was discussed above:
strcpy(s1, "one");
strcpy(s2, "two");
strcpy(s3, "three");
There are also other reasons for using strings. For example, the null-terminated C String does not
check if the array is out of bound and that contributes to many string problems encountered by C++
programmers, experienced and inexperienced alike. Following is an example showing you how to
use the string class to manipulate strings. The example gives you a feeling of how string objects
work, which means you do not need to remember how it really works.
#include <iostream>
#include <string>
int main()
string str2("ABCDEFG");
// demonstrate length()
cout << "Length of str1 is: " << str1.length() << endl;
return 0;
Initial strings:
str2: ABCDEFG
Lab Tasks
Question # 1
Question # 2
Write a program in C++ to read a sentence and replace lowercase characters by uppercase and
vice-versa.
Question # 3
Question # 4
Write a C++ program to concatenate one string after the other without using any library function.
Question # 5
Write a C++ program which stores names of five cities and print the names of only those cities