Professional Documents
Culture Documents
Lab
(CL 1004)
LABORATORY MANUAL
Fall 2023
LAB 02
C-strings
Engr. Arslan Ahmed
______________________________________
LAB ENGINEER SIGNATURE & DATE
MARKS AWARDED: /10
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:
Need to know size beforehand, i-e. static type No Need to know size beforehand
arrays
If you follow the rule of array initialization, then you can write the above statement as
follows:
char greeting[] = "Hello";
Fall 2023: 2
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
C-Strings LAB 02
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.
'\0'};
return 0;
}
When the above code is compiled and executed, it produces the following result.
1
strcpy(s1, s2);
Copies string s2 into string s1.
Fall 2023: 3
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
C-Strings LAB 02
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.
#include <iostream>
#include <cstring>
int main () {
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
Fall 2023: 4
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
C-Strings LAB 02
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>
int main () {
return 0;
}
When the above code is compiled and executed, it produces result something as follows.
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:
Fall 2023: 5
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
C-Strings LAB 02
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declaring a string variable
string str;
cout << "Enter a string: ";
getline(cin, str);
There are four ways to take input from user in a string or character array.
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)
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];
Fall 2023: 6
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
C-Strings LAB 02
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)
Example 1:
Fall 2023: 7
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
C-Strings LAB 02
#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
Fall 2023: 8
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
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;
Program Code
Fall 2023: 9
NUCES, ISLAMABAD Page of 10
Object Oriented Programming Lab
C-Strings LAB 02
2. Exercise:
A. Ask user to enter a sentence. Make a program that reverses the given string. Use Character
Arrays.
B. 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().
C. Ask user to enter a sentence using character array. For example; “I love Pak”
i. Find the length of the largest word in this string. Answer should be 4 in this case.
ii. Display the largest word as well. Answer should be love in this case.
D. Ask user to enter a sentence. Remove 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-string/string respectively. Make your own function
Remove_Number().
Note: - Make your own functions in above program.
Page
Spring 2023: 10
NUCES, ISLAMABAD of 10
Programming Fundamentals Lab