You are on page 1of 3

Keth Iralex Cabalda BES 043

COC-FA-BSCE2-04 Module #14

C Strings

Activity 1: What I Know Chart

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.

3. What are the C string functions?


- strcat , strchr . strcmp , strcpy , strlen ,strncat , strncmp and etc…

Activity 3: Skill-building Activities

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){

cout << "Password granted!" << endl;


}
else
{
cout << "Password denied! " << endl;
}
return 0;
}
2. Upgrade the program in the number 1. The password program will have three trials in entering the
password if it is wrong. If the password has been entered incorrectly the first and second time, it will
display “Wrong password! Try again…”.

- #include <iostream>

#include <string>

using namespace std;

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 )

cout << "Access granted";

}
else
{
cout << "Wrong password! Try again...";
}
}

Activity 5: Check for understanding

B 1.. int strlen(const char *s)

A 2. char *strcpy(char *s, const char *t)

C 3. char *strncpy(char *s, const char *t, unsigned n)

E 4. int strcmp(const char *s, const char *t)


C 5. int strncmp(const char *s, const char *t, int n)

Activity 6: Thinking about Learning

Three things you learned:


1. Write an program using C string function.
2. I learned about C string.
3. I learned how to do with C string

Two things that you’d like to learn more about:


1. More about writing program using C string.
2. More examples in C string.

One question you still have:


1. Is it true that the C string functions strcmp() and strncmp() are strict with lower and upper case
letters?

You might also like