Name: ___________________________________

Lab/Hwk C-Strings and C++ Strings

C-Strings

  1. Write a function string_replace(char *s, char c) that replaces every occurrence of the character c in the string s with the uppercase character of c.

    You will need to include: <cstring> and <cctype>

    Test your function on several different inputs.


  2. Fill in the missing parts of the following program. The program checks whether the password that a user enters, is a valid password.
    Password rules:
    1. The password must be at least 8 characters long.
    2. The password must contain at least: one alpha character [a-zA-Z];
    3. one numeric character [0-9];
    4. one special character from this set: ! $ % ?
    5. The password must not contain spaces
    6. The password must not begin with an exclamation [!] or a question mark [?]
    7. The first 3 characters of the password must not be the same character
    
    #include <iostream>
    #include <cctype>
    #include <cstring>
    
    using namespace std;
    
    //function prototypes
    void displayRules();
    bool validatePasswd(char *passwd);
    
    
    int main() 
    {
      char newpasswd[25];
      bool OK = false;
    
      while (!OK) 
      {
      cout << "Enter a valid password. To see the password rules, type ? and press enter."<< endl;
    
      cin >> newpasswd;
    
    //check whether the user entered a ?
    
    // FILL IN MISSING CODE TO DISPLAY RULES IF THE USER ENTERED a ?
    
    
      else {
         OK = validatePasswd(newpasswd);
         if (OK)
           cout << "Your password has been accepted. "<< endl;
         }
      } // end while
    
    return 0;
    } // end main
    
    void displayRules()
    {
    
    // FILL IN THE MISSING CODE TO DISPLAY THE RULES
    
    
    return;
    }
    
    bool validatePasswd(char *passwd)
    {
    bool OK=false;
    
    //Check length
    if (//FILL IN THE CONDITION) 
       { 
       cout << "Length must be at least 8" << endl;
       return false;
       }
    
    //Check for alpha character
    //FILL IN CODE HERE. Note: The next 3 tests are very similar to one another.
    
    //Check for numeric character
    //FILL IN CODE HERE
    
    //Check for special character
    //FILL IN CODE HERE
    
    // Check that the password does not begin with ? or !
    if (passwd[0]=='?' || passwd[0]=='!') return false;
       
    // Check that the first 3 characters are not equivalent
    //FILL IN CODE HERE
    
    return true;
    }
    
    
    

C++ Strings

  1. Write a program that reads in two strings, one on each line, and returns the number of times that the second string appears in the first string. You will use both the getline() function, and the string::find() function.

    Note: You will use the following version of the find function:
    size_t find ( const string& str, size_t pos = 0 );
    
    Where pos=0 is the default, but once you are searching for the second occurrence, you will set pos to answer+1.

    If the string str is not found, the member value npos is returned by the find function. npos is accessed by saying:
    string::npos


  2. Write a program that asks the user how many students are in a class, and then dynamically allocates an array of strings for the students' names.

    Write two functions: (1) to read in the students' names into the array, (2) to display the list of student names.