Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Laboratory 10: Using C-String and String Classes • Write a program that has an a

ID: 3825148 • Letter: L

Question

Laboratory 10: Using C-String and String Classes
• Write a program that has an array of 11 string objects that hold
people’s names and phone numbers. Use the following data:
• "Alejandra Cruz, 555-1223",
• "Joe Looney, 555-0097",
• "Geri Palmer, 555-8787",
• "Li Chen, 555-1212",
• "Holly Gaddis, 555-8878",
• "Sam Wiggins, 555-0998",
• "Bob Kain, 555-8712",
• "Tim Haynes, 555-7676",
• "Warren Gaddis, 555-9037",
• "Jean James, 555-4939",
• "Ron Palmer, 555-2783"

Laboratory 10: Using C-String and String Classes
• The program should ask the user to enter a name or partial name to search
for in the array.
• Any entries in the array that match the string entered should be displayed.
• For example, if the user enters “Palmer” the program should display the
following names from the list:
• Geri Palmer, 555-8787
• Ron Palmer, 555-2783
• This can be done using the C-String Class.
• Program 10-6, shown in the appendix to this Lab, shows a similar example, indicating
how this might be done using the C-String Class.
• This can also be done using the String Class.
• For that you will want to make use of the find() method shown on page 589 of
Gaddis, and also shown in the appendix to this lab.
• Tip: If a string Member Function fails to perform the procedure it was asked to do, it
returns -1 to the program calling the function.

Laboratory 10: Using C-String and String Classes
• Using Program 10-6, modify it so that it is able to do a search for the
names in our contact list.
• Note that the Array used here is a two dimensional Array of char
• The first dimension (row dimension) represents the number of contacts in the Array
• The second dimension contains the Array of char holding the Contact information for one
person
• Once you have that working, call the instructor so that you may receive
credit for having accomplished this.
• Now, in the remaining time, repeat this search using the String Class
instead. You may find that you will be able to execute this program
using fewer steps than those used for the C-String class.
• Note that the Array used here is a one dimensional Array of Strings.
• Note also that you will be including the string Class instead of the cstring Class.
• Again, call the instructor when you have this version working OK.

// This program uses the strstr function to search an array.
#include <iostream>
#include <cstring> // For strstr
using namespace std;
int main()
{
// Constants for array lengths
const int NUM_PRODS = 5; // Number of products
const int LENGTH = 27; // String length
// Array of products
char products[NUM_PRODS][LENGTH] =
{ "TV327 31 inch Television",
"CD257 CD Player",
"TA677 Answering Machine",
"CS109 Car Stereo",
"PC955 Personal Computer" };
char lookUp[LENGTH]; // To hold user's input
char *strPtr = NULL; // To point to the found product
int index; // Loop counter

// Prompt the usr for a product number.
cout << " Product Database ";
cout << "Enter a product number to search for: ";
cin.getline(lookUp, LENGTH);
// Search the array for a matching substring
for (index = 0; index < NUM_PRODS; index++)
{
strPtr = strstr(products[index], lookUp);
if (strPtr != NULL)
break;
}
// If a matching substring was found, display the product info.
if (strPtr != NULL)
cout << products[index] << endl;
else
cout << "No matching product was found. ";
return 0;
}

mystring.find(str, x);
• Returns the first position at or
beyond position x where the
string str is found in mystring.
• str may be either a string object
or a character array.

Explanation / Answer

//lab10

#include<iostream>
#include<string>
#include<string.h>
using namespace std;

int main()
{
   int ret ;
   char contact[11][40] = { "Alejandra Cruz, 555-1223", "Joe Looney, 555-0097", "Geri Palmer, 555-8787","Li Chen, 555-1212", "Holly Gaddis, 555-8878", "Sam Wiggins, 555-0998", "Bob Kain, 555-8712", "Tim Haynes, 555-7676", "Warren Gaddis, 555-9037", "Jean James, 555-4939","Ron Palmer, 55-2783" };
   char str[20];
   cout << "Enter the name or partial name to search: ";
   cin >> str;
   char *p;
   for (int i = 0; i < 11; i++)
   {
       p = strstr(contact[i], str);
       if (p)
       {
           cout << contact[i] << endl;
       }
      
   }
  
}

----------------------------------------------------------

//output lab10

Enter the name or partial name to search: Palmer
Geri Palmer, 555-8787
Ron Palmer, 55-2783

-----------------------------------------

//lab11

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <cstring> // For strstr
using namespace std;

class mystring
{
   char str[40];
public:
   mystring()
   {
       strcpy(str, "");
   }
   void assign(char s[])
   {
       strcpy(str , s);
   }
   int find(char s[])
   {
       char *p;

       p = strstr(str, s);
       if (p)
       {
           cout << str << endl;
           for (int i = 0; i < strlen(str); i++)
           {
               cout << str[i] << endl;
               if (str[i] == s[0])
                   return i;
           }
       }
       return 0;

   }
   void print()
   {
       cout << str << endl;
   }
};
int main()
{
   // Constants for array lengths
   const int NUM_PRODS = 5; // Number of products
   const int LENGTH = 27; // String length
   // Array of products
   char products[NUM_PRODS][LENGTH] =
   { "TV327 31 inch Television",
   "CD257 CD Player",
   "TA677 Answering Machine",
   "CS109 Car Stereo",
   "PC955 Personal Computer" };
   char lookUp[LENGTH]; // To hold user's input
   char *strPtr = NULL; // To point to the found product
   int index; // Loop counter
   // Prompt the usr for a product number.
   cout << " Product Database ";
   cout << "Enter a product number to search for: ";
   cin.getline(lookUp, LENGTH);
   // Search the array for a matching substring
   for (index = 0; index < NUM_PRODS; index++)
   {
       strPtr = strstr(products[index], lookUp);
       if (strPtr != NULL)
           break;
   }
   // If a matching substring was found, display the product info.
   if (strPtr != NULL)
       cout << products[index] << endl;
   else
       cout << "No matching product was found. ";
   //test mystring
   mystring *str;
   str = new mystring[11];
   //inistialize string objects
   for (int i = 0; i < 11; i++)
   {
       str[i].assign(products[i]);
   }
   //find position of first occurance
   for (int i = 0; i < 11; i++)
   {
       int found;
       if ( found = str[i].find("31"))
       {
           cout << "String 31 find at position: " << found << endl;
       }
   }
   return 0;
}

-------------------------------------------------

//output2

Product Database

Enter a product number to search for: 31
TV327 31 inch Television
TV327 31 inch Television
T
V
3
String 31 find at position: 2