String Manipulation Functions: Name: - Year & Sec
String Manipulation Functions: Name: - Year & Sec
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.
6. strrev() – this string function reverses all the characters in the string.
Syntax: strrev(str);
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);
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
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);
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
Program Code:
#include<iostream.h>
#include<cstring.h>
int main()
{
string name;
return 0;
}
Output:
#include<iostream.h>
#include<cstring.h>
int main()
{
string name;
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2
name = to_lower(name);
cout << "lowercase = " << name << endl;
name = to_upper(name);
cout << "uppercase = " << name << endl;
return 0;
}
Output:
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;
len = name.length();
return 0;
}
Output:
sname.append(", ");
sname.append(fname);
return 0;
}
Output:
if( name1.compare(name2) == 0)
{
cout << "Similar";
}
else
{
cout << "Not Similar";
}
return 0;
}
Module 1: String Manipulation Functions CC 123C: COMPUTER PROGRAMMING 2
Output:
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
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
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
w.insert(2,"@@@");
return 0;
}
Output:
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;
w.remove(2);
return 0;
}
Output:
Program Code:
#include<iostream.h>
#include<cstring.h>
int main()
{
string w;
w.remove(2,3);
return 0;
}
Output:
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;
new_word = original_word.substr(0);
return 0;
}
Output:
Program Code:
#include<iostream.h>
#include<cstring.h>
int main()
{
string original_word, new_word;
new_word = original_word.substr(6,6);
return 0;
}
Output: