0% found this document useful (0 votes)
58 views

String LAB 9

This document provides an overview of strings in C++. It defines what a string is, how they are declared and initialized, and how to perform basic operations on strings like input/output, concatenation, comparison, and reversing. It includes examples of declaring, initializing, reading, printing, concatenating, comparing, and reversing strings. It also summarizes some common string library functions like getline(), strcat(), strcpy(), strcmp(), strlen(), and strrev().

Uploaded by

Fahad Ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

String LAB 9

This document provides an overview of strings in C++. It defines what a string is, how they are declared and initialized, and how to perform basic operations on strings like input/output, concatenation, comparison, and reversing. It includes examples of declaring, initializing, reading, printing, concatenating, comparing, and reversing strings. It also summarizes some common string library functions like getline(), strcat(), strcpy(), strcmp(), strlen(), and strrev().

Uploaded by

Fahad Ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab # 9 Strings

(CLO 3)

13.1 Objective:
Learn how to declare, initialize and use one-dimensional character arrays (string).
13.2 Scope:
The student should know the following:
1. What is string?
2. How to Read and Print Strings.
3. Some important built-in string functions. How to use them?
13.3 Useful Concepts:
What is String?
A string is any sequence of characters enclosed in double quotes. There is no separate data
type for strings as integer, float or double. The string data type can be considered as a char array. The
difference is that a character variable can hold only one character but a string can have more than
one character in a character array. For example, a string called name with 9 characters can be
declared as:
char name [9] = “I like C” ;
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";
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.

Note: Strings are stored as arrays of characters and have a special ‘\0’ termination character called
NULL appended (attached) to them to signify the end of the string.
Note that if the number of characters including the ‘\0’ is more than the size of the array the
results will be unpredictable. However, if the size of the array is more than the number of characters the
extra spaces after the last ‘\0’ character are kept blank and not referred because the string ends at ‘\
0’. So, always make sure that the size of the array is sufficient for the string. For example, the above
declaration would be wrong if we write
char name [8] = “I like C” ;
The size can be ignored also. In that case the size is considered as that of the number of characters
specified in the declaration.
char name [] = “I like C” ;

String Input/Output:
The easiest way to input strings is by using the C++ library function getline(). The
getline()function reads a string of characters entered at the keyboard until you strike the enter key
(carriage return). The carriage return does not become part of the string; instead a null terminator ‘\0’ is
placed at the end.
For example, in the following program fragment
char str[80] ;
cin.getline(str,80);
and if the user enters
Muslim Abbas
and presses the enter key the string variable str has the characters “Muslim Abbas” with ‘\0’
appended at the end but is not displayed.
To display the string, we can use
cout<<str;

Built-in String Functions:


Large collections of string processing functions are provided in C++ through cstring file. So,
include the cstring file in the programs to use these functions. To use the string functions make sure
that the size of the array is sufficient so that the strings are terminated with the ‘\0’ character or the
functions will not work properly.
strcat ( string1, string2 );
The strcat function concatenates or joins the string1 and string2. A copy of
string2 is put at the end of the string1. Make sure that string1 size is long enough to hold the
resulting string (string1 + string2).
Example: char string1[81] = “abc”, string2 [] = “def” ;
strcat ( string1, string2) ;
cout<<string1; // outputs “abcdef” which is stored in string1

strcpy (string1, string2);


The strcpy function copies string2 into string1. Again make sure that string1 size is long
enough to hold the resulting string.
Example: char string1 [81], string2 [] = “memory”;
strcpy (string1, string2) ;
cout<< string1; //outputs “memory” copied into string1

strcmp (string1, string2 );

The strcmp function compares the string1 to string2 and returns an integer value to
show the status of the comparison. A value of 0 indicates that the two strings are identical. A value of less
than 0 shows that string1 is lexicographically (according to alphabetic ordering) less than string2.
A value of greater than 0 shows that string1 is lexicographically (according to alphabetic ordering)
greater than string2.
strlen(string1);

The strlen function returns an integer equal to the length of the stored string including blanks,
not including the termination character.
strchr(string, ch );
The strchr function searches string for the first occurrence of ch. This function only tells
whether the string contains ch or not and it will not tell the position of the ch in the string if found.

13.4 Examples:

Example 13.1: Write a program that prints a string.

#include <iostream>
using namespace std;

int main()
{
char array[20] = "Mir Zakriya Javed";
cout<<array<<endl;
return 0;
}

The output of the program is:

Example 13.2: Write a program that input a string.

#include <iostream>
using namespace std;

int main()
{
char array[20];

cout<<"Enter a string: ";


cin.getline(array, 20);

cout<<"You entered the string: "<<array<<endl;

return 0;
}
The output of the program is:

Example 13.3: Write a Program that Inputs a string using cin only.

#include <iostream>
using namespace std;

int main()
{
char array[20];

cout<<"Enter a string: ";


cin>>array;

cout<<"You entered the string: "<<array<<endl;

return 0;
}

The output of the program is:

Note that the cin can only input single word strings, to receive strings containing spaces use the
getline function.

Example 13.4: Operations on Strings

#include <iostream>
#include <string>

using namespace std;

int main () {

string str1 = "Muslim";


string str2 = "Abbas";
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;
}

The output of the program is:

Example 13.5: Demonstation of Library functions.

#include <iostream>
#include <cstring>

using namespace std;

int main () {

char str1[10] = "Muslim";


char str2[10] = "Abbas";
char str3[10];
int len ;

// 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;
}

The output of the program is:


Example 13.6: Write a program to compare two strings using strcmp. Also make use of gets() to read the
string.

#include <iostream>
#include <cstring>

using namespace std;

int main () {

char a[100], b[100];

cout<<"Enter the first string: ";


gets(a);

cout<<"Enter the second string: ";


gets(b);

if (strcmp(a,b) == 0)
cout<<"Entered strings are equal.\n";
else
cout<<"Entered strings are not equal.\n";

return 0;
}

The output of the program is:

Example 13.7: Write a program that prints string in reverse order.

#include <iostream>
#include <cstring>

using namespace std;

int main () {
char arr[100];
cout<<"Enter a string to reverse\n";
gets(arr);
strrev(arr);
cout<<"Reverse of entered string is: "<<arr<<endl;

return 0;
}

The output of the program is:

13.5 Exercises for lab


Exercise 13.1 Write a program that reads a string and print the number of vowels letters.
Exercise 13.2 Write a Program number that reads a string and print upper case and number of
lower-case letters.
Exercise 13.3 Write a program that reads a string and print the string with the first letter
capitalized and the remaining in lower case.

13.6 Home Work


1. Write a program that reads a string and print the reverse of that string:
e.g. User Enter string: This is a cat
Output: - tac a si siht
2. Write a program that reads a string and print the string’s words in reverse order:
e.g. User Enter string: This is a cat
Output: - cat a is This

Hints: - Use two arrays of same size. One is used to take value from user and other is used to
store reverse string.

You might also like