Professional Documents
Culture Documents
Keth Iralex Cabalda BES 043 (Module 14)
Keth Iralex Cabalda BES 043 (Module 14)
C Strings
1. What is a C string?
- A string is a sequence of characters. C and C++ implement strings as arrays of char.
2.What can we do with C strings?
- It is used for storing text/characters.
1.Write a simple password program. The password is “Hi”. The program will only check the password
once. It will display “Password granted!” if the password is correct or else it will display “Password
denied!”. Use any C string functions available.
-
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pass = "Hi";
string input;
cout << "What is your password: ";
cin >> input;
if (input==pass){
- #include <iostream>
#include <string>
int main()
{
string password;
int attempts = 1;
cout << "Please enter your password: ";
getline(cin, password, '\n');
while ( password != "Hi" && attempts < 3 )
{
cout << "Please try again: ";
getline(cin, password, '\n');
attempts++;
}
if ( attempts < 3 )
}
else
{
cout << "Wrong password! Try again...";
}
}