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

Activity 5.2. Character and String Built-In Functions

The document contains multiple code snippets demonstrating the use of string functions in C++ like strcpy(), strncpy(), strchr(), strrchr(), strcmp(), and strcmpi(). The code samples copy strings, search for characters within strings, reverse strings, and compare strings for equality. The output of each code sample is also displayed.

Uploaded by

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

Activity 5.2. Character and String Built-In Functions

The document contains multiple code snippets demonstrating the use of string functions in C++ like strcpy(), strncpy(), strchr(), strrchr(), strcmp(), and strcmpi(). The code samples copy strings, search for characters within strings, reverse strings, and compare strings for equality. The output of each code sample is also displayed.

Uploaded by

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

Activity 5.4.

Multiple Array with Function Copy

Cadelina,Rovic M.

ITE 001B-EE21S2

CODE: OUTPUT:
#include <cstring>
#include <iostream>
using namespace std;
int main(){
char str1[20];
char str2[20];
cout <<"Enter string: ";
cin >> str1;
cout <<"Enter string: ";
cin >> str2;
cout <<strncpy(str1,str2, 3);
cout<<str1;
}
CODE: OUTPUT:
#include <cstring>
#include <iostream>
using namespace std;
int main(){
char str1[20];
char str2[20];
cout <<"Enter string: ";
cin >> str1;
cout <<"Enter string: ";
cin >> str2;
cout <<strcpy(str1,str2);
}
CODE: OUTPUT:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
char str1[20];
cout << "Enter string: ";
cin >> str1;
std::reverse(str1, str1 + std::strlen(str1));
cout << "Reversed: " << str1 << endl;
}
CODE: OUTPUT:
#include <cstring>
#include <iostream>
using namespace std;
int main(){
char str1[20], str2[20];
cout<<"Enter string:";
cin>>str1;
cout<< strchr (str1, 's')<<endl;
}
CODE: OUTPUT:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char str1[20], str2[20];
char *str3;
cout <<"Enter string:";
cin>>str1;
cout <<strchr(str1,'a')<<endl;
cout <<strrchr(str1,'a')<<endl;
cout <<strchr(str1,'o')<<endl;
}
CODE: OUTPUT:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char* str1 = new char[20];
char* str2 = new char[20];
cout << "Enter two strings: ";
cin >> str1 >> str2;
if (strcmp(str1, str2) == 0) {
cout << "Strings are equal!";
} else {
cout << "Strings are not equal!";
}
}else {
cout<<"String are not equal!";
}
}
CODE: OUTPUT:
#include <iostream>

#include <cstring>

using namespace std;

int main(){

int len,x;

char uname[20];

char pwd[20];

char u[20]={'r','i','s'};

char PWD[20]={'1','2','3'};

cout<<"Please, Enter username:\n";

cin>>uname;

cout<<"Please, Enter password:\n";

cin>>pwd;

int user= strcmpi(uname,u);

int pass= strcmpi(pwd, PWD);

if (user==0&&(pass ==0))

cout <<"welcome to the system";

else

cout<<"invalid login. try again!";

return 0;

}
CODE: OUTPUT:

#include <cstring>
#include <iostream>
using namespace std;
int main() {
char *str1 = new char[20];
char str2[] = {'h', 'e', 'l', 'l', 'o'};
cout << "Enter string: ";
cin.getline(str1, 20);
if (strcmp(str1, str2) == 0) {
cout << "Strings are the same";
} else {
cout << "Strings are not the same";
}
return 0;
}

You might also like