You are on page 1of 44

MODULE 3

CHARACTER AND STRING


MANIPULATION
LESSON #1
C-String Character
Manipulation Functions
Upon completion of this subtopic, you will be able to:
• Understand the CSTRING library and its functions
• Create programs using CSTRING functions
• Apply CSTRING function in solving problems related to
string manipulation
A C-String is usually declared as an array of char.

A valid C string requires the presence of a terminating "null


character" (a character with ASCII value 0, usually represented
by the character literal '\0').
Here are some examples of declaring C strings as arrays
of char:
char s1[20] = { 'h', 'e', 'l', 'l', 'o', '\0' };
char s2[20] = "hello";
char s3[20] = "";
Here is another example of declaring a C string:
char name[10] = "Lance";

The following diagram shows how the string name is


represented in memory:

L a n c e \0
Since char is a built-in data type, no header file is required to
create a C string.

The C++ <cstring> header file declares a set of functions to


work with C style string (null terminated byte strings).
strcpy copies character string from source to destination
strncpy copies a specified bytes of characters from source to
destination.

strcat appends a copy of a string to the end of another string


strncat appends a specified number of characters of a string to
the end of another string
strlen returns length of given string

strcmp compares two null terminating string. The


comparison is done lexicographically.

strncmp compares a specified number of characters of two


null terminating strings. The comparison is done l
exicographically.
Using strcpy and strncpy functions
#include <iostream>
#include <cstring>
using namespace std;
int main()
{ char x[]="good morning ";
char y[25], z[15];
cout<<"The string character in array x is " << x<<endl;
cout<<"The string character in array y is " << strcpy(y,x)<<endl;
strncpy(z,y,4);
z[5]='\0';
cout<<"The string character in array z is " << z<<endl;
system("pause>0");
return 0;
}
Using strlen and strcmp functions
OUTPUT:
The member function getline can be used to read a line of input
and place the string of characters on that line into a C-string
variable.

syntax:
cin.getline(String_Var, Max_Characters + 1);
OUTPUT:
OUTPUT:
get function allows your program to read in one character
of input and store it in a variable of type char
SAMPLE CODE:
put This function member is analogous to the member
function get except that it is used for output rather than
input. The function put allows your program to output one
character
SAMPLE CODE:
OUTPUT:
This header declares a set of functions to classify and
transform individual characters.

Character conversion functions


tolower Convert uppercase letter to lowercase
toupper Convert lowercase letter to uppercase
Character classification functions
isupper Check if character is uppercase letter
islower Check if character is lowercase letter

isalpha Check if character is alphabetic


isdigit Check if character is decimal digit

isalnum Check if character is alphanumeric


Isspace Check if character is a white space
Character classification functions
ispunct Check if character is a punctuation character
isprint Check if character is printable

isgraph Check if character has graphical representation


Isctrl Check if character is a control character
toupper(Char_Exp)
Returns the uppercase version of Char_Exp (as
value of type int).
tolower(Char_Exp)
Returns the lowercase version of
Char_Exp (as value of type int).
isupper(Char_Exp)
Returns true provided Char_Exp is
an uppercase letter; otherwise,
returns false.
islower(Char_Exp)
Returns true provided Char_Exp is
an lowercase letter; otherwise,
returns false.
isalpha(Char_Exp)
Returns true provided Char_Exp is
a letter of the alphabet; otherwise
returns false.
isdigit(Char_Exp)
Returns true provided Char_Exp is
one of the digits ‘0’ through ‘9’;
otherwise, returns false.
isalnum(Char_Exp)
Returns true provided Char_Exp is
either a letter or a digit; otherwise,
returns false.
isspace(Char_Exp)
Returns true provided Char_Exp is
a whitespace character, such as
the blank or newline character,
otherwise, returns false.
isprint(Char_Exp)
Returns true provided Char_Exp is
a printing characters includes blank
space; otherwise returns false.
isgraph(Char_Exp)
Returns true provided Char_Exp is
a printing characters; otherwise
returns false.
ispunct(Char_Exp)
Returns true provided Char_Exp is
a printing character other than
whitespace, a digit, or a letter;
otherwise returns false.
https://www.programiz.com/cpp-programming/library-function/cctype
https://www.programiz.com/cpp-programming/library-function/cstring
http://www.cplusplus.com/reference/cctype/
http://www.cplusplus.com/reference/cstring/
https://www.tutorialspoint.com/cpp_standard_library/cpp_locale_ctype.htm
https://en.cppreference.com/w/cpp/header/cctype
LESSON#2
String Class Functions
Upon completion of this subtopic, you will be able to:
• Understand the STRING library and its functions
• Create programs using STRING functions
• Apply STRING function in solving problems related to
string manipulation
A sequence of characters can be represented using an object
of a class in C++.

The class which provides a definition to do so is called


a String class.

String header <string> needs to be included in the program


to use the String class. Let us understand how to declare and
assign a value to a string.
Example 1:
string str;
str = "hello world";

Example 1 declares 'str' as a string and initializes it to a value


"hello world".
The length() function calculates the number of characters in
a string.

Example: string str = "hello";


str.length();

The function length() calculates the number of characters in


the string including spaces and punctuation marks. In this
case, the length of the string is 5.
Values present in two strings can be checked for equality either
using the equality operator '==' or the compare() function.

Example : string str1 = "computer";


string str2 = "science";
if (str1 == str2)
cout << "strings are equal"
else
cout << "strings are not equal";
Example:
string str1 = "computer";
string str2 = 'science";
if (str1.compare(str2) == 0)
cout << "strings are equal"
else
cout << "strings are not equal"
Two string can be joined either by using the addition operator '+'
or the append() function.

string str1 = "computer";


string str2 = "science";
string str3 = str1 + " " + str2;
cout << str3;
Two string can be joined either by using the addition operator '+'
or the append() function.

string str1 = "computer";


string str2 = "science";
string str3 = str1 + " " + str2;
cout << str3;
Example:
string str1 = "computer";
string str2 = "science";
str1.append(" ");
str1.append(str2);

Two strings are joined using append function. In the example, the
append function first joins str1 with space and then the string
str1 is joined with 'str2'.
http://www.cplusplus.com/reference/string/
https://www.programiz.com/cpp-programming/strings
https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
https://www.w3schools.com/cpp/cpp_strings.asp
https://www.geeksforgeeks.org/c-string-class-and-its-applications/

You might also like