fork download
  1. // Attached: Lab 2 - Program #2
  2. // ===========================================================
  3. // File: Lab2_Program2
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <cstring> // for c-string functions
  11. using namespace std;
  12.  
  13. // ==== main ====================================================
  14. //
  15. // This program uses c_strings (character arrays) to read a
  16. // user's name and state, displays character counts, and
  17. // compares the first and last names.
  18. //
  19. // =============================================================
  20. int main()
  21. {
  22. // Variable declarations (empty c-strings)
  23. char firstName[20] = "";
  24. char lastName[20] = "";
  25. char fullName[40] = "";
  26. char city[30] = "";
  27.  
  28. // Input
  29. cout << "Enter your first name: ";
  30. cin.getline(firstName, 20);
  31.  
  32. cout << "Enter your last name: ";
  33. cin.getline(lastName, 20);
  34.  
  35. cout << "Enter the state you live in: ";
  36. cin.getline(city, 30);
  37.  
  38. // Processing
  39. strcpy(fullName, firstName);
  40. strcat(fullName, " ");
  41. strcat(fullName, lastName);
  42.  
  43. // Output
  44. cout << "Hi " << fullName << ". So you live in " << city << "." << endl;
  45.  
  46. cout << "Your first name has " << strlen(firstName)
  47. << " characters," << endl;
  48.  
  49. cout << "and your last name has " << strlen(lastName)
  50. << " characters." << endl;
  51.  
  52. if (strcmp(firstName, lastName) == 0)
  53. {
  54. cout << "Your first and last names are the same." << endl;
  55. }
  56. else
  57. {
  58. cout << "Your first and last names are different." << endl;
  59. }
  60.  
  61. cout << "Press any key to continue . . .";
  62. cin.get();
  63.  
  64. return 0;
  65. } // end of main()
  66. // =============================================================
  67.  
Success #stdin #stdout 0.01s 5320KB
stdin
Elaine
Torrez
California
stdout
Enter your first name: Enter your last name: Enter the state you live in: Hi Elaine Torrez. So you live in California.
Your first name has 6 characters,
and your last name has 6 characters.
Your first and last names are different.
Press any key to continue . . .