Commit a29e2c93 authored by Michael's avatar Michael

michael.davityan:exercise_07_32v4

parent 95bdb431
......@@ -2,7 +2,7 @@ progname=exercise_07_32
CC=g++
CFLAGS=-Wall -Wextra -Werror -g3
all: test1 test2 test3 test4
all: test1 test2
debug/$(progname): debug/$(progname).o
$(CC) $(CFLAGS) $< -o $@
......
#include <iostream>
#include <unistd.h>
#include <string>
#include <cassert>
bool
palindromeCheck(const std::string word, int sizeOfWord, int firstLetterIndex = 0)
palindromeCheck(const std::string &word, size_t lastIdx, size_t firstIdx = 0)
{
if (firstLetterIndex == sizeOfWord) {
if (firstIdx == lastIdx) {
return true;
}
if (word[firstLetterIndex] != word[sizeOfWord]) {
if (' ' == word[firstIdx]) {
return palindromeCheck(word, lastIdx, firstIdx + 1);
}
if (' ' == word[lastIdx]) {
return palindromeCheck(word, lastIdx - 1, firstIdx);
}
if (word[firstIdx] != word[lastIdx]) {
return false;
}
return palindromeCheck(word, sizeOfWord - 1, firstLetterIndex + 1);
return palindromeCheck(word, lastIdx - 1, firstIdx + 1);
}
int
......@@ -25,7 +32,11 @@ main()
}
std::string word;
std::cin >> word;
int sizeOfWord = word.size();
std::cout << palindromeCheck(word, sizeOfWord - 1) << std::endl;
const size_t lastIdx = word.size() - 1;
if (true == palindromeCheck(word, lastIdx)) {
std::cout << "The string is palindrome\n";
} else {
std::cout << "The string is not palindrome" << std::endl;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment