You are on page 1of 19

Module 1: String Manipulation

Functions
TABLE OF CONTENTS

STRING FUNCTIONS........................................................................................................1
THE BASIC STRING FUNCTIONS...............................................................................1
STRCPY ()......................................................................................................................... 2
STRLW R (), STRUPR (), STRREV (), STRLEN ().........................................................................4
STRCMP ()........................................................................................................................ 5
STRCMPI ()....................................................................................................................... 6
STRNCPY ()....................................................................................................................... 7
S TRING M ANIPULATING FUNCTIONS ...............................................................................9
COMMONLY USED FUNCTIONS UNDER THE CSTRING.H:...............................................................9
A. TO_LOWER()...................................................................................................................9
B. TO_UPPER().................................................................................................................... 9
C. LENGTH ().....................................................................................................................10
D. APPEND ()....................................................................................................................11
E. COMPARE ()..................................................................................................................12
F. CONTAINS ()..................................................................................................................13
G. FIND_FIRST_OF ()..........................................................................................................14
H. FIND_LAST_OF ()...........................................................................................................15
I. INSERT ()....................................................................................................................... 16
J. REMOVE ()....................................................................................................................16
K. SUBSTR ()..................................................................................................................... 17

Name: ____________________
Year & Sec: ________________
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

STRING FUNCTIONS
In C++ Programming, string is considered as a sequence or series of
characters and treated as single data item. A string data is enclosed
within double quotes. It includes letters, symbols, digits, and control
characters. Strings are used in programs to store and process text data
manipulation.

Since string is a sequence or an array of characters, it can be declared


as char (character) data type and an array variable. The Borland Turbo
C++ has library routines for string manipulations called <string.h>.Every
time, you want to use some of these string functions, we are required to
include this string function library to make our program runs properly.

Example of string data:


“I love Gravy!”
“Jhacy Parish”
“#183 3 r d Street”
“$ 2007”
“Mrs.”
“5,202,007”

THE BASIC STRING FUNCTIONS


1. strlen() – this function returns the length of the string. Syntax:
strlen(string);

2. strcpy() – this string function copies the content of string2 to string1.


The str1 and str2 can be variable or string data (value).
Syntax: strcpy(str1,str2);

3. strcat() – this string function can concatenates the strings. It appends


string2 to end of string1.
Syntax: strcat(str1,str2);

4. strlwr() – this string converts all the uppercase letters in string to


lowercase.
Syntax: strlwr(str);
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

5. strupr() – this string converts all the lowercase letters in string to


uppercase.
Syntax: strupr (str);

6. strrev() – this string function reverses all the characters in the string.
Syntax: strrev(str);

7. strcmp() – this string functions compares two strings. If


string1>string2, the function returns a positive value; if
string1<string2, the function returns a negative value; if
string1==string2, the function returns a zero value.
Syntax: strcmp(str1,str2);

8. strcmpi() - this string functions compares two strings and ignores


whether an uppercase or lowercase letters are being compared.
Lowercase and uppercase letters are treated equal or the same.
Syntax: strcmpi(str1,str2);

9. strncpy() – this string function copies only a portion(size) of strings


into string1.
Syntax: strncpy(str1,str2,size);

10. toupper() – this string function converts an input lowercase letter


into its uppercase equivalent.
Syntax: toupper(vletter);

11. tolower() – this string function converts an input uppercase letter


into its lowercase equivalent.
Syntax: tolower (vletter);

strcpy()
Example 1
This program will demonstrate how to copy a string of data into the
variable.
Program Code:
#include <iostream.h>
#include <cstring.h>
int main()
{
char phrase1[] = "No man is an island";
char phrase2[80];
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

strcpy(phrase2,phrase1);

cout << "OUTPUT 1: " << phrase1 << endl;


cout << "OUTPUT 2: " << phrase2 << endl;
return 0;
}

Output:

Example 2
This program will demonstrate how to copy a string of data into the
variable.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
char gn[10],fn[10];
char cgn[10],cfn[10];
cout<<"Enter your given name: ";
cin>>gn;
cout<<"Enter your family name: ";
cin>>fn;
strcpy(cgn,gn);
strcpy(cfn,fn);
cout<<cgn<<" "<<cfn;
return 0;
}

Output:

Example 3
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

This program demonstrates how the value of string 1 is overwritten by


the value of string2.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
char str1[20],str2[20];
strcpy(str1,"I Love");
strcpy(str2,"C++ Programming");
strcpy(str1,str2);
cout<<str1<<endl;
cout<<str2;
return 0;
}

Output:

strlwr(),strupr(),strrev(),strlen()
Example 4
This program demonstrates how the values of strings are being converted
into lowercase or uppercase and how it is reversed. Then it computes
the length of the string.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
char str1[20],str2[20];
int length1,length2;

strcpy(str1,"Bjarne");
strcpy(str2,"Stroustrup");
length1=strlen(str1);
length2=strlen(str2);

cout<<"The length of string1, " <<str1 <<" is:


"<<length1<<endl;
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

cout<<"The length of string2, " <<str2 <<" is:


"<<length2<<endl;

strcat(str1,str2);
cout<<"The new value of string 1 is: "<<str1<<endl;
cout<<"The new value of string 2 is: "<<str2<<endl;

length1=strlen(str1);
cout<<"The new length of string 1 is: "<<length1<<endl;

length2=strlen(str2);
cout<<"The new length of string 2 is: "<<length2<<endl;

strlwr(str1);
strupr(str2);
cout<<"The string 1 in all lowercase: "<<str1<<endl;
cout<<"The string 2 in all uppercase: "<<str2<<endl;

strrev(str2);
cout<<"The string 2 in reverse order: "<<str2<<endl;
return 0;
}

Output:

strcmp()
Example 5
This program demonstrates how the string compare strcmp() function
works.
Program Code:
#include<iostream.h>
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

#include<cstring.h>
#include<conio.h>
main()
{
char str[20];
int c;
clrscr();
cout<<"Enter your password: ";
cin>>str;
c=strcmp(str,"kcahj");
if(c==0)
cout<<"Welcome to the system!";
else
cout<<"Intruder has been detected!";
return 0;
}

Output:

strcmpi()
Example 6:
This program demonstrates how strcmpi() works and how it differs from
strcmp(). The strcmpi() ignores the lowercase or uppercase difference
between two compared values. Capital letters and lowercase letters are
treated equal and the same.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
char str[20];
int c;
cout<<"Enter your password: ";
cin>>str;
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

c=strcmpi(str,"kcahj");
if(c==0)
cout<<"Welcome to the system!";
else
cout<<"Intruder has been detected!";
return 0;
}

Output:

strncpy()
Example 3
This program demonstrates how strncpy() function works technically.
This string function copies only a portion (a number of characters) of the
given string.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
char target[20];
char source[20]="I love to program";
strncpy(target, source,5);
target[5]='\0';
cout<<target;
return 0;
}
Output:
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

STRING MANIPULATING FUNCTIONS


cstring.h - Declares several string-manipulation routines.
getline() – a function under the iostream.h which is use to read strings
including white spaces.

Program Code:
#include<iostream.h>
#include<cstring.h>

int main()
{
string name;

cout << "Enter your name:";


getline(cin,name);

cout << "Hello " << name;

return 0;
}

Output:

COMMONLY USED FUNCTIONS UNDER THE CSTRING.H:


a. to_lower() – Changes the string to lowercase.
b. to_upper() – Changes the string to uppercase.
Program Code:

#include<iostream.h>
#include<cstring.h>
int main()
{
string name;
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

cout << "Enter your name: ";


getline(cin,name);

cout << endl;

name = to_lower(name);
cout << "lowercase = " << name << endl;

name = to_upper(name);
cout << "uppercase = " << name << endl;

return 0;
}
Output:

c. length () – Returns the number of characters in the target string.


Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string name;
int len;

cout << "Enter your name: ";


getline(cin,name);

cout << endl;

len = name.length();
cout << "number of characters = " << len << endl;

return 0;
}
Output:
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string name;
int len,ctr;

cout << "Enter your name: ";


getline(cin,name);

cout << endl;

len = name.length();

for(ctr=0; ctr<len; ctr++)


{
cout << name[ctr] << ", ";
}

return 0;
}
Output:

d. append () – Appends a string to the target string.


Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string fname, sname;

cout << "Enter your firstname: ";


getline(cin,fname);

cout << "Enter your surname: ";


getline(cin,sname);
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

sname.append(", ");
sname.append(fname);

cout << "\nHello " << sname;

return 0;
}
Output:

e. compare () – Compares the target string to the string s. Compare


returns an integer less than, equal to, or greater than 0, depending on
whether the target string is less than, equal to, or greater than s.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string name1, name2;

cout << "Enter name1: ";


getline(cin,name1);

cout << "Enter name2: ";


getline(cin,name2);

if( name1.compare(name2) == 0)
{
cout << "Similar";
}

else
{
cout << "Not Similar";
}

return 0;
}
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

Output:

f. contains () – A command use to locate a string from another string. This


function returns 1 if a string is found in the target string, 0 otherwise.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string w1, w2;

cout << "Enter word #1: ";


getline(cin,w1);

cout << "Enter word #2: ";


getline(cin,w2);

cout << endl;

if( w1.contains(w2)!=0 )
{
cout << w2 << " was found at " << w1;
}
else
{
cout << w2 << " was not found at " << w1;
}

return 0;
}
Output:
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

g. find_first_of () – Locates the first occurrence in the target string of any


character contained in string s. If the search is successful find_first_of
returns the character location. If the search fails, find_first_of returns a
negative value.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string w1, w2;
int pos;

cout << "Enter word #1: ";


getline(cin,w1);

cout << "Enter word #2: ";


getline(cin,w2);

cout << endl;

pos = w1.find_first_of(w2);

if(pos<0)
{
cout << w2 << " was not found in " << w1;
}
else
{
cout << w2 << " was found at index " << pos;
}

return 0;
}
Output:
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

h. find_last_of () – Locates the last occurrence in the target string of any


character contained in string s. If the search is successful find_last_of
returns the character location. If the search fails, find_last_of returns a
negative value.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string w1, w2;
int pos;

cout << "Enter word #1: ";


getline(cin,w1);

cout << "Enter word #2: ";


getline(cin,w2);

cout << endl;

pos = w1.find_last_of(w2);

if(pos<0)
{
cout << w2 << " was not found in " << w1;
}
else
{
cout << w2 << " was found at index " << pos;
}

return 0;
}

Output:
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

i. insert () – Inserts string at a specified position in the target string.


Insert returns a reference to the resulting string.
Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
string w;

cout << "Enter a word: ";


getline(cin,w);

w.insert(2,"@@@");

cout << "new word = " << w;

return 0;
}
Output:

j. remove () – Inserts string at a specified position in the target string.


Insert returns a reference to the resulting string.

Form 1 string.remove(position );
Form 2 string.remove(position, length );

Description
Form 1: Removes the characters from position to the end of the target
string and returns a reference to the resulting string.
Form 2: Removes at most length of characters from the target string
beginning at position and returns a reference to the resulting
string.

Program Code:
#include<iostream.h>
#include<cstring.h>
main()
{
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

string w;

cout << "Enter a word: ";


getline(cin,w);

w.remove(2);

cout << "new word = " << w;

return 0;
}
Output:

Program Code:
#include<iostream.h>
#include<cstring.h>

int main()
{
string w;

cout << "Enter a word: ";


getline(cin,w);

w.remove(2,3);

cout << "new word = " << w;

return 0;
}
Output:

k. substr () – Creates a string containing a copy of the characters from the


target string.
Form 1: string.substr(position );
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

Form 2: string.substr(position, size);

Description:
Form 1: Creates a string containing a copy of the characters from
position to the end of the target string.
Form 2: Creates a string containing a copy of not more than size
characters from position to the end of the target string.

Program Code:
#include<iostream.h>
#include<cstring.h>
int main()
{
string original_word, new_word;

cout << "Enter a word: ";


getline(cin,original_word);

new_word = original_word.substr(0);

cout << "\nSubstring = " << new_word;

return 0;
}

Output:

Program Code:
#include<iostream.h>
#include<cstring.h>
int main()
{
string original_word, new_word;

cout << "Enter a word: ";


getline(cin,original_word);
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2

new_word = original_word.substr(6,6);

cout << "\nSubstring = " << new_word;

return 0;
}

Output:

You might also like