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

Lesson2 String Manipulation Functions

The document discusses various string manipulation functions in C++. It describes functions for converting strings to uppercase/lowercase, reversing strings, finding string lengths, copying strings, concatenating strings, and comparing strings. These functions include strlwr(), strupr(), strrev(), strlen(), strcpy(), strncpy(), strcat(), strcmp(), and strcmpi(). Examples are provided to demonstrate the usage of each function.

Uploaded by

Satoru Fujinuma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lesson2 String Manipulation Functions

The document discusses various string manipulation functions in C++. It describes functions for converting strings to uppercase/lowercase, reversing strings, finding string lengths, copying strings, concatenating strings, and comparing strings. These functions include strlwr(), strupr(), strrev(), strlen(), strcpy(), strncpy(), strcat(), strcmp(), and strcmpi(). Examples are provided to demonstrate the usage of each function.

Uploaded by

Satoru Fujinuma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT

3 STRINGS

IT 105 Computer Programming 2


LESSON 2
String Manipulation Functions

IT 105 Computer Programming 2


String Manipulation Functions
C++ provides a set of function that can be used for string
manipulation. The header file cstring must be included to use
these functions
• Converting string to uppercase or lowercase
strlwr ( ) – converts all the uppercase letters in string to
lowercase.
Syntax : strlwr ( string);
strupr ( ) – converts all the lowercase letters in string to
uppercase.
Syntax : strupr ( string);

IT 105 Computer Programming 2


String Manipulation Functions
Example:
char s1[10]= “HELLO”; Output : hello
strlwr(s1);
cout<< “Output :”<<s1<<endl;

char s1[10]= “hello”; Output : HELLO


strupr(s1);
cout<< “Output :”<<s1<<endl;

IT 105 Computer Programming 2


String Manipulation Functions
• Reversing the string
strrev ( ) – reverses all the characters in the string.

Syntax : strrev( string);


Example: char s1[10]= “HELLO”;
strrev(s1);
cout<< “Output :”<<s1<<endl;

Output : OLLEH

IT 105 Computer Programming 2


String Manipulation Functions
• Finding the length of string
strlen ( ) – returns the length of the string.
Syntax : strlen ( string constant or variable)
Example:
char s1[10]= “HELLO”; Output : 5
int len;
len = strlen(s1);
cout<<”Output: “<<len;

IT 105 Computer Programming 2


String Manipulation Functions
• Copying string
Suppose you want to store the content of one string variable
into another, the use of assignment operator (=) is not allowed.
Instead, you may use strcpy function.

strcpy ( ) – copies the content of string.


Syntax : strcpy ( string1, string2);

// copies the content of string2 to string1

IT 105 Computer Programming 2


String Manipulation Functions
Example:
char s1[10]; char s1[10]= “HELLO”;
char s2[10]; char s2[10]= “GOOD DAY”;
strcpy( s1,”BSU”); strcpy( s2,s1);
strcpy( s2,”CICT”); cout<< “String 1:”<<s1<<endl;
cout<< “String 1:”<<s1<<endl; cout<< “String 2:”<<s2<<endl;
cout<< “String 2:”<<s2<<endl;
Output: Output:

String 1: BSU String 1: HELLO


String 2: CICT String 2: HELLO

IT 105 Computer Programming 2


String Manipulation Functions
• Copying part of string
strncpy ( ) – copies only a portion ( size) of strings.
Syntax : strncpy (string1, string2,size);
char s1[15]= “HAPPY”;
char s2[15]= “GOOD DAY”;
strncpy(s1,s2, 3);
cout<< “String 1:”<<s1<<endl;
cout<< “String 2:”<<s2<<endl;
Output:
String 1: GOOPY
String 2: GOOD DAY

IT 105 Computer Programming 2


String Manipulation Functions
• Concatenating Strings
strcat ( ) – concatenates two strings.
Syntax: strcat ( string1, string2) ;
// It appends string2 to the end of string1. The size of string1 variable must be enough
to hold the concatenated string when declared

char s1[15]= “HELLO”;


char s2[15]= “GOOD DAY”;
strcat(s1,s2);
cout<< “String 1:”<<s1<<endl;
cout<< “String 2:”<<s2<<endl;
Output:
String 1: HELLOGOOD DAY
String 2: GOOD DAY
IT 105 Computer Programming 2
String Manipulation Functions
• Comparing Strings

strcmp ( ) – compares two strings.


Syntax : strcmp (string1, string2);

• If string 1 > string2, the function returns a positive value;


• If string 1 <string2, the function returns a negative value;
• if string 1 == string2, the function returns a zero value.

strcmpi ( ) – compares two strings and ignores whether an uppercase


or lowercase letters are being compared .
Syntax : strcmpi (string1, string2);

IT 105 Computer Programming 2


String Manipulation Functions
char s1[5]="ABC", s2[5] ="BCD”, s3[5]=“bcd”;
int w,x ,y,z;
w=strcmp(s1,s2);
x=strcmp(s2,s1);
y=strcmp(s2,s3);
z=strcmpi(s2,s3);

cout<<" Output 1:" <<w<<endl;


cout<<" Output 2:" <<x<<endl;
cout<<" Output 3:" <<y<<endl;
cout<<" Output 4:" <<z<<endl;

Output 1: - 1
Output 2: 1
Output 3: - 1
Output 4: 0
IT 105 Computer Programming 2

You might also like