Object Oriented Programming
(CL 1004)
LABORATORY MANUAL
Fall 2025
LAB 02
C-Strings
Engr. Fasih Ahmad
Muhammad Ashnab Safdar 24I-6500 CE-A
STUDENT NAME ROLL NO SEC
______________________________________
LAB ENGINEER SIGNATURE & DATE
MARKS AWARDED: /10
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Last Edited by: Engr. Nimra Fatima Version: 2.01
Prepared by: Engr. Sana Saleh Date: 12 Aug, 2017
Verified by: Engr. Aamer Munir Date: 19 Jan, 2019
C-Strings LAB 02
LAB 02 C-Strings
Lab Objectives:
1. To learn and revise the concepts of C-style character string.
2. To learn about C-strings and strings datatype.
Software Required:
Dev C++
Introduction:
1) Character Array Vs String:
Character Array Strings
Need to know size beforehand, i-e. static No Need-to-know size beforehand
type arrays
Terminated with a special character ‘\0’ No terminating extra character
C++ provides following two types of string representations −
i. The C-style character string.
ii. The string data type introduced with Standard C++.
i) The The C-style character string:
The C-style character string originated within the C language and continues to be
supported within C++. This string is actually a one-dimensional array of characters
which is terminated by a null character '\0'. Thus, a null-terminated string contains
the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word
"Hello". To hold the null character at the end of the array, the size of the character
array containing the string is one more than the number of characters in the word
"Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization, then you can write the above statement as
follows:
char greeting[] = "Hello";
Fall 2025:
NUCES, ISLAMABAD Page 2 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
Following is the memory presentation of above defined string in C/C++
Actually, you do not place the null character at the end of a string constant. The C++
compiler automatically places the '\0' at the end of the string when it initializes the array. Let
us try to print above-mentioned string.
#include <iostream>
using namespace std;
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result.
Greeting message: Hello
C++ supports a wide range of functions that manipulate null-terminated strings, you need to
add <cstring> library before using below functions.
Sr.N Function & Purpose
o
1
strcpy(s1, s2);
Fall 2025:
NUCES, ISLAMABAD Page 3 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
Copies string s2 into string s1.
2
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3
strlen(s1);
Returns the length of string s1.
4
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5
Strrev(s1);
Reverses the order of characters in the null-terminated string s1.
6
Strlwr(s1);
Converts all uppercase characters in the null-terminated string s1 to their lowercase
equivalents, modifying the original string.
7
Strupr(s1);
Converts all lowercase characters in the null-terminated string s1 to their uppercase
equivalents, modifying the original string.
8
strcasecmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2, but this
is case insensitive.
Following example makes use of few of the above-mentioned functions.
#include <iostream>
#include <cstring>
using namespace std;
int main () {
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
Fall 2025:
NUCES, ISLAMABAD Page 4 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
// copy str1 into str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// concatenates str1 and str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// total lenghth of str1 after concatenation
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
return 0;
}
When the above code is compiled and executed, it produces result something as follows.
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
ii) The String data type in C++
The standard C++ library provides a string data type that supports all the operations
mentioned above, additionally much more functionality. Let us check the following example:
#include <iostream>
#include <string>
using namespace std;
int main () {
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
// copy str1 into str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// concatenates str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// total length of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}
When the above code is compiled and executed, it produces result something as follows.
Fall 2025:
NUCES, ISLAMABAD Page 5 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
iii) getline () function to take input string:
In C++, we can take input string using getline () function. Following program shows how to
use this function:
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declaring a string variable
string str;
cout << "Enter a string: ";
getline(cin, str);
cout << "You entered: " << str << endl;
return 0;
}
iv) C-String Input Functions:
There are four ways to take input from user in a string or character array.
a. >> operator (only for words without spaces)
b. cin.get(Use only for C-string)
c. getline (Use only for string)
a. cin (>> operator)
The >> operator may be used when programmer wants to read the next non-blank
space characters entered by the user into a character or character array. Any
printable characters that follow the first space will be ignored and will not be stored in
the variable.
b. cin.get() Function
The unformatted get function works like the >> operator with two exceptions. First,
the get function includes white-space characters, whereas the extractor excludes
white space. Second, the get function is less likely to cause a tied output stream (cout,
for example) to be flushed. Syntax is as follow:
cin.get(char_array_name, char_array_length)
Fall 2025:
NUCES, ISLAMABAD Page 6 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
A variation of the get function specifies a buffer address and the maximum number of
characters to read. This is useful for limiting the number of characters sent to a
specific variable, as this example shows:
#include <iostream.h>
void main()
{
char line[25];
cout << " Type a line\n";
cin.get( line, 25 );
cout << ' ' << line;
}
In this example, you can type up to 24 characters and a terminating character. Any
remaining characters can be extracted later.
c. cin.getline() Function
The getline function is similar to the get function. Both functions allow a third
argument that specifies the terminating character for input. The default value is the
newline character. Both functions reserve one character for the required terminating
character. However, get leaves the terminating character in the stream and getline
removes the terminating character. Syntax of cin.getline for character array is given
below:
cin.getline(char_array_name, char,array_length)
Syntax for string is given below:
getline(cin, string_name)
v) C-String Manipulation Functions:
Some of the C-string manipulation functions are described below:
Function Effect
strcpy(s1, s2) Copies the string s2 into the string variable s1. The length
of s1 should be at least as large as s2
strlen(s) Returns the length of the string s, excluding the null
character.
strcat(s1,s2) Appends a copy of the source string to
the destination string. The terminating null character
in destination is overwritten by the first character
of source, and a null-character is included at the end of
the new string formed by the concatenation of both
in destination.
strcmp(s1,s2) Returns a negative number if string s1 is less than string
s2, returns zero if the two strings are equal, and returns a
positive number is string s1 is greater than string s2. It
compares Lexicographically.
Fall 2025:
NUCES, ISLAMABAD Page 7 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
Concatenation of C++ C-string:
The concatenation of C++ C-string using strcat() function of string is as follow.
Example 1:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str[80];
strcpy (str,"These ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
cout<<(str);
return 0;
}
Console Output
These strings are concatenated.
Fall 2025:
NUCES, ISLAMABAD Page 8 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
Example 2:
Following code use all of the C-string functions described above. Run this code.
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
int main()
{
char string1[20];
char string2[20];
char string3[] = "Hello";
cout << "Enter the first string\n";
cin >> string1;
cout << "Enter the second string\n";
cin >> string2;
cout << setw(20)<<"Comparing both strings\n";
int compare = strcmp(string1, string2); //String cmp
if (compare==1)
cout << "First string is greater than string 2\
n";
else if (compare == 0)
cout << "Both strings are same/equal\n";
else
cout << "String 2 is larger as compared to
string 1\n";
cout << setw(20)<<"\nUsing string Copy Function\n";
cout << "Copy string3 to string 1\n";
strcpy(string1, string3); //String copy
cout<<string1<<endl;
cout << setw(20) << "\nUsing string Length Function\
n";
cout << "Length of string 1 is " << strlen(string1)
<< endl;
cout << "Length of string 2 is " << strlen(string2)
<< endl;
cout << setw(20) << "\nUsing string Concatenation
Function\n";
strcat(string1, " ");
strcat(string1, string3);
cout << string1 << endl;
}
Program Code
Fall 2025:
NUCES, ISLAMABAD Page 9 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
Exercise:
A. Create a program in C++ that performs the following tasks:
i. Accepts a string input from the user and stores it in a character array.
ii. Determines and outputs the size (length) of the character array (excluding
the null terminator).
iii. Displays the content of the character array back to the user.
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
int length = input.length();
char* sentence = new char[length + 1]; // dynamic memory allocation
strcpy(sentence, input.c_str());
system("cls");
cout << "You entered: " << sentence << endl;
cout << "\nLength of the string : " << length << endl;
delete[] sentence;
system("pause");
return 0;
Fall 2025:
NUCES, ISLAMABAD Page 10 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
B. Ask the user to enter a sentence. Write a program that reverses the given string
and stores it in the same string using character arrays.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
int length = input.length();
char* sentence = new char[length + 1];
strcpy(sentence, input.c_str());
for (int i = 0; i < length / 2; i++) { // we can also use strrev() function
char temp = sentence[i];
sentence[i] = sentence[length - 1 - i];
sentence[length - 1 - i] = temp;
}
cout << "Reversed sentence: " << sentence << endl;
delete[] sentence;
return 0;
}
Fall 2025:
NUCES, ISLAMABAD Page 11 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
C. Write a C++ program to capitalize the first letter of each word of a given string.
Words must be separated by only one space. Make your own function
Capital_Word().
#include <iostream>
#include <string>
using namespace std;
void Capital_Word(string &s) {
int n = s.length();
if (s[0] >= 'a' && s[0] <= 'z') {
s[0] = s[0] - 32;
for (int i = 1; i < n; i++) {
if (s[i - 1] == ' ' && (s[i] >= 'a' && s[i] <= 'z')) {
s[i] = s[i] - 32;
}};
int main() {
string sentence;
cout << "Enter a sentence: ";
getline(cin, sentence);
Fall 2025:
NUCES, ISLAMABAD Page 12 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
Capital_Word(sentence);
cout << "After capitalization: " << sentence << endl;
return 0;
D. Ask user to enter a sentence using character array. For example; “I love Pak”.
Find the length of the largest word in this string. Answer should be 4 in this case.
Note: - Make your own functions name max_length_word().
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int max_length_word(const char sentence[]) {
int maxLen = 0;
int currLen = 0;
for (int i = 0; sentence[i] != '\0'; ++i) {
if (sentence[i] != ' ' && sentence[i] != '\t') {
++currLen;
} else {
if (currLen > maxLen) maxLen = currLen;
currLen = 0;
Fall 2025:
NUCES, ISLAMABAD Page 13 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
if (currLen > maxLen) maxLen = currLen;
return maxLen;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
int size = static_cast<int>(input.length());
char* sentence = new char[size + 1];
strcpy(sentence, input.c_str());
int result = max_length_word(sentence);
cout << "Length of the largest word: " << result << endl;
delete[] sentence;
return 0;
Fall 2025:
NUCES, ISLAMABAD Page 14 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
E. Ask user to enter a sentence. Separate all numbers entered by user from the
sentence. Implement this program, using C-string/string i.e. character array. Store the
numbers and alphabets in separate C-strings respectively. Make your own function
Separate_Number().
#include <iostream>
#include <string>
using namespace std;
void Separate_Number(char sentence[], char alphabets[], char numbers[]) {
int aIndex = 0, nIndex = 0;
for (int i = 0; sentence[i] != '\0'; i++) {
if (sentence[i] >= '0' && sentence[i] <= '9') {
numbers[nIndex++] = sentence[i];
}
else if ((sentence[i] >= 'A' && sentence[i] <= 'Z') ||
(sentence[i] >= 'a' && sentence[i] <= 'z') ||
sentence[i] == ' ') {
alphabets[aIndex++] = sentence[i];
}
}
alphabets[aIndex] = '\0';
numbers[nIndex] = '\0';
}
int main() {
string sen;
cout << "Enter a sentence: ";
getline(cin, sen);
int SIZE = sen.length();
char* sentence = new char[SIZE + 1];
char* alphabets = new char[SIZE + 1];
char* numbers = new char[SIZE + 1];
for (int i = 0; i < SIZE; i++) {
sentence[i] = sen[i];
}
sentence[SIZE] = '\0';
Separate_Number(sentence, alphabets, numbers);
cout << "Alphabets only: " << alphabets << endl;
cout << "Numbers only: " << numbers << endl;
delete[] sentence;
delete[] alphabets;
delete[] numbers;
Fall 2025:
NUCES, ISLAMABAD Page 15 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
system("pause");
return 0;
}
ask the user to enter a sentence containing both uppercase and lowercase letters.
Write a program that converts all uppercase letters to lowercase and vice versa using
C-string/character array. Store the modified sentence in the same C-string. Make your
own function Toggle_Case().
Hint:
'a' (97) – 'A' (65) = 32
...
'A' (65) + 32 = 'a' (97)
char ch = 'D'; // ASCII 68
ch = ch + 32; // 68 + 32 = 100 → 'd'
char ch2 = 'k'; // ASCII 107
ch2 = ch2 - 32; // 107 - 32 = 75 → 'K'
You can view the ASCII table here: https://www.ascii-code.com/
#include <iostream>
#include <string>
using namespace std;
void Toggle_Case(char sentence[]) {
for (int i = 0; sentence[i] != '\0'; i++) {
if (sentence[i] >= 'A' && sentence[i] <= 'Z') {
sentence[i] = sentence[i] + 32;
}
else if (sentence[i] >= 'a' && sentence[i] <= 'z') {
sentence[i] = sentence[i] - 32;
}
}
}
int main() {
string sen;
cout << "Enter a sentence: ";
getline(cin, sen);
int size = sen.length();
char* sentence = new char[size + 1];
Fall 2025:
NUCES, ISLAMABAD Page 16 of 17
OOP Lab
Department of Computer Engineering
C-Strings LAB 02
for (int i = 0; i < size; i++) {
sentence[i] = sen[i];
}
sentence[size] = '\0';
Toggle_Case(sentence);
cout << "Toggled case sentence: " << sentence << endl;
delete[] sentence;
system("pause");
return 0;
}
Fall 2025:
NUCES, ISLAMABAD Page 17 of 17
OOP Lab
Department of Computer Engineering