Activity 5.2. Character and String Built-In Functions
Activity 5.2. Character and String Built-In Functions
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>
int main(){
int len,x;
char uname[20];
char pwd[20];
char u[20]={'r','i','s'};
char PWD[20]={'1','2','3'};
cin>>uname;
cin>>pwd;
if (user==0&&(pass ==0))
else
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;
}