1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Osman Zakir
// 10 / 8 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 18 Sections 18.7.1, 18.7.2 and 18.7.3 Palindromes using std::string, Palindromes using arrays and Palindromes using pointers
// Writing and using function to check if given std::string or C-style is a palindrome
// 10 / 15 / 2017
// Chapter 18 Exercise 8
// Exercise Specifications
/**
* Rewrite all the functions in §18.7 to use the approach of making a back-
* ward copy of the string and then comparing; for example, take "home" ,
* generate "emoh" , and compare those two strings to see that they are
* different, so home isn’t a palindrome.
*/
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "../../cust_std_lib_facilities.h"
#include <iostream>
#include <algorithm>
#include <vld.h>
#include <cstring>
#include <string>
bool is_palindrome(const std::string &s);
bool is_palindrome(const char s[], int n);
bool is_palindrome(const char *first, const char *last);
std::istream &read_word(std::istream &is, char *buffer, int max);
int main()
{
std::string str;
char c_string[5];
char c_string2[5];
std::cout << "Give me three strings: ";
std::cin >> str;
std::cin >> c_string;
std::cin >> c_string2;
for (int i = 0; i < strlen(c_string) && strlen(c_string2); ++i)
{
if (c_string[i] >= c_string[strlen(c_string) - 1] || c_string2[i] >= c_string2[strlen(c_string2) - 1])
{
char *new_str1 = new char[strlen(c_string) + 1];
char *new_str2 = new char[strlen(c_string2) + 1];
memcpy(new_str1, c_string, strlen(c_string) + 1);
memcpy(new_str2, c_string2, strlen(c_string2) + 1);
new_str1[strlen(c_string) + 1] = '\0';
new_str2[strlen(c_string2) + 1] = '\0';
strcpy(c_string, new_str1);
strcpy(c_string2, new_str2);
delete[] new_str1;
delete[] new_str2;
new_str1 = nullptr;
new_str2 = nullptr;
}
}
std::cout << "The string " << str << " is";
if (is_palindrome(str))
{
std::cout << " a";
}
else
{
std::cout << " not a";
}
std::cout << " palindrome\n";
std::cout << "The string " << c_string << " is";
if (is_palindrome(c_string, strlen(c_string)))
{
std::cout << " a";
}
else
{
std::cout << " not a";
}
std::cout << " palindrome\n";
std::cout << "The string " << c_string2 << " is";
if (is_palindrome(&c_string2[0], &c_string2[strlen(c_string2) - 1]))
{
std::cout << " a";
}
else
{
std::cout << "not a";
}
std::cout << "palindrome\n";
keep_window_open();
}
bool is_palindrome(const std::string &s)
{
std::string copy_s{ s };
std::reverse(copy_s.begin(), copy_s.end());
if (copy_s != s)
{
return false;
}
return true;
}
bool is_palindrome(const char s[], int n)
// s points to the first character of an array of n characters
{
char *copy_s = new char[n + 1];
for (int i = n - 1, j = 0; i >= 0; --i, ++j)
{
copy_s[j] = s[i];
}
if (strncmp(s, copy_s, n) != 0)
{
delete[] copy_s;
copy_s = nullptr;
return false;
}
delete[] copy_s;
copy_s = nullptr;
return true;
}
bool is_palindrome(const char *first, const char *last)
// first points to the first letter, last to the last letter
{
const std::size_t str_length = last - first;
char *str = new char[str_length + 1];
for (std::size_t i = 0; i < str_length; ++i)
{
str[i] = first[i];
}
str[str_length + 1] = '\0';
std::cout << "\ndebug (is_palindrome() number 3: " << str << '\n';
char *str_copy = new char[str_length + 1];
for (std::size_t i = strlen(str), j = 0; i > 0; ++j, --i)
{
str_copy[j] = str[i];
}
str_copy[strlen(str)] = '\0';
std::cout << "\ndebug: (is_palindrome() number 3: " << str_copy << '\n';
if (strncmp(str, str_copy, strlen(str)) != 0)
{
delete[] str;
str = nullptr;
return false;
}
delete[] str;
str = nullptr;
return true;
}
std::istream &read_word(std::istream &is, char *buffer, int max)
// read at most max-1 characters from is into buffer
{
is.width(max); // read at most max-1 characters in the next >>
is >> buffer; // read whitespace-terminated word,
// add NULL-terminating zero after last character read into buffer
return is;
}
|