Suggestions with Throw dice program - if-statement

I am trying to build a new application where I can with the help of a parameter know how many times the dice must be thrown before you can get the number of sixes from the parameter. Hope you understand.I am quite new in this so please help me.
Here is my code:
package programe;
import java.util.Random;
public class Dice {
public static void main(String[] args) {
sixes(5);
}
public static void sixes(int x){
int roll = 0;
int dice;
int count;
Random random = new Random();
for(count = 1; count <= roll; count++){
dice = 1 + random.nextInt(6);
System.out.println("It takes " + roll + "before you get" + x + " sixes in a row");
}
}
}

The problem didnt let me think about other stuff so i decided to solve it and its done.
Here is the code:
public class Dice {
public static String sixes(int x){
int rolls = 0;
String sixesRow =" ";
String result = "";
Random r = new Random();
while(true){
rolls++;
int rDice = r.nextInt(7);
if (rDice == 6){
if(sixesRow.charAt(sixesRow.length()-1) == '6' || sixesRow.charAt(0) == ' '){
sixesRow.replace(' ', '6');
String sixesRowCurrent = sixesRow.concat("6");
sixesRow = sixesRowCurrent;
}
if(rowCheck(sixesRow, x)){
result = "Took " + rolls + " throws to get " + x + " sixes in a row!";
break;
}
}
else{
String sixesRowCurrent = sixesRow.concat("0");
sixesRow = sixesRowCurrent;
}
}
return result;
}
public static boolean rowCheck(String sixesRow, int x){
boolean xTimesRow = false;
String testString = "";
for(int i = 0; i < x; i++){
String loopString = testString.concat("6");
testString = loopString;
}
if(sixesRow.contains(testString)){
xTimesRow = true;
}
return xTimesRow;
}
public static void main(String[] args){
System.out.println("Please insert the amount of sixes in a row: ");
Scanner sc = new Scanner(System.in);
int sixes = sc.nextInt();
System.out.println(sixes(sixes));
}
}

Related

What is the normal way to debug nested for loop?

I am writing code for Leetcode problem 38. Count and Say. It doesn't pass the cases, so I add some cout to debug. Please tell me is there a normal way to debug nested for loop, where should I add the cout expression. I don't want to know how to modify the code to pass the cases.
Here is my code:
class Solution {
public:
string countAndSay(int n) {
string cur("1");
while (--n) {
string tmp = cur;
string next;
for (int i = 0; i < tmp.size();) {
cout << "i:" << i << endl;
int count = 1;
for (int j = i + 1; j < tmp.size(); j++) {
if (tmp[j] != tmp[0]) {
break;
}
count++;
}
cout << "count:" << count << endl;
next += std::to_string(count) + tmp[0];
cout << "cur:" << cur << endl;
i += count;
}
cur = next;
cout << n << cur << endl;
}
return cur;
}
};
You're gonna have to use a debugger for that, and step by step go through your algorithm to find the bugs. It's hard to debug someone else's algorithm.
This'll pass through:
#include <string>
struct Solution {
static const std::string countAndSay(int n) {
if (not n) {
return "";
}
std::string res = "1";
while (--n) {
std::string curr = "";
for (int index = 0; index < res.size(); index++) {
int count = 1;
while ((index + 1 < res.size()) and (res[index] == res[index + 1])) {
count++;
index++;
}
curr += std::to_string(count) + res[index];
}
res = curr;
}
return res;
}
};
Java Solutions
class Solution {
public String countAndSay(int n) {
if (n == 1)
return "1";
String prev = countAndSay(n - 1);
StringBuilder str = new StringBuilder();
int i = 0;
while (i < prev.length()) {
char curr = prev.charAt(i);
int j = 0;
while (i + j < prev.length() && prev.charAt(i + j) == curr)
j++;
str.append(j);
str.append(curr);
i += j;
}
return str.toString();
}
}
Here is one of LeetCode's solutions using regex:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
public String countAndSay(int n) {
String currSeq = "1";
// Pattern to match the repetitive digits
String regexPattern = "(.)\\1*";
Pattern pattern = Pattern.compile(regexPattern);
for (int i = 1; i < n; ++i) {
Matcher m = pattern.matcher(currSeq);
StringBuffer nextSeq = new StringBuffer();
// each group contains identical and adjacent digits
while (m.find()) {
nextSeq.append(m.group().length() + String.valueOf(m.group().charAt(0)));
}
// prepare for the next iteration
currSeq = nextSeq.toString();
}
return currSeq;
}
}
and here's another LeetCode's solution also using Sliding Window:
class Solution {
public String countAndSay(int n) {
LinkedList<Integer> prevSeq = new LinkedList<Integer>();
prevSeq.add(1);
// Using -1 as the delimiter
prevSeq.add(-1);
List<Integer> finalSeq = this.nextSequence(n, prevSeq);
StringBuffer seqStr = new StringBuffer();
for (Integer digit : finalSeq) {
seqStr.append(String.valueOf(digit));
}
return seqStr.toString();
}
protected LinkedList<Integer> nextSequence(int n, LinkedList<Integer> prevSeq) {
if (n <= 1) {
// remove the delimiter before return
prevSeq.pollLast();
return prevSeq;
}
LinkedList<Integer> nextSeq = new LinkedList<Integer>();
Integer prevDigit = null;
Integer digitCnt = 0;
for (Integer digit : prevSeq) {
if (prevDigit == null) {
prevDigit = digit;
digitCnt += 1;
} else if (digit == prevDigit) {
// in the middle of the sub-sequence
digitCnt += 1;
} else {
// end of a sub-sequence
nextSeq.add(digitCnt);
nextSeq.add(prevDigit);
// reset for the next sub-sequence
prevDigit = digit;
digitCnt = 1;
}
}
// add the delimiter for the next recursion
nextSeq.add(-1);
return this.nextSequence(n - 1, nextSeq);
}
}
References
For additional details, you can see the Discussion Board. There are plenty of accepted solutions with a variety of languages and explanations, efficient algorithms, as well as asymptotic time/space complexity analysis1, 2 in there.

Converting Decimal in String Format to a Double Using Stacks

this project is currently due by tonight and I have tried my best on it so far. If I could get any guidance on to how I should continue working on it that would be greatly appreciated. I have this topic I am covering for a project: "Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format."
I am first of all, confused as to how a stack would help make a decimal in string format into a decimal in numeric format. How would that work internally in the program? Second of all, my code that I created is not working for some reason and I'm unsure as to of why.
I tried looking into questions that were asked on stack overflow and other websites but nothing could answer my question.
#include <iostream>
#include <cassert>
#include <string>
using namespace std;
template <class Type>
class stackADT
{
public:
virtual void initializeStack() = 0;
virtual bool isEmptyStack() const = 0;
virtual bool isFullStack() const = 0;
virtual void push(const Type& newItem) = 0;
virtual Type top() const = 0;
virtual void pop() = 0;
};
template <class Type>
class stackType: public stackADT<Type>
{
private:
int maxStackSize;
int stackTop;
public:
Type *list;
void initializeStack()
{
stackTop = 0;
cout << "stackTop " << stackTop << endl;
}
void print()
{
for(int i=0; i<stackTop; i++)
{
cout << list[i] << endl;
}
}
bool isEmptyStack() const
{
return(stackTop == 0);
}
bool isFullStack() const
{
return(stackTop == maxStackSize);
}
void push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
{
cout << "Cannot add to a full stack." << endl;
}
cout << "stacktop: " << stackTop << endl;
system("pause");
}
Type top() const
{
assert(stackTop != 0); //if stack is empty, terminate the program.
return list[stackTop - 1];
}
Type getList() const
{
assert(stackTop != 0); //if stack is empty, terminate the program.
return *list;
}
void pop()
{
if (!isEmptyStack())
stackTop--;
else
cout << "Cannot remove from an empty stack." << endl;
cout << "pop: " << stackTop << endl;
}
stackType(int stackSize = 100)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must be positive." << endl;
cout << "Creating an array of size 100." << endl;
maxStackSize = 100;
}
else
{
maxStackSize = stackSize;
// cout << "maxStackSize " << maxStackSize << endl;
}
stackTop = 0;
list = new Type[maxStackSize];
}
stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
~stackType()
{
delete [] list;
}
const stackType<Type>& operator=(const stackType<Type>& otherStack)
{
if (this != &otherStack)
{
copyStack(otherStack);
}
return *this;
}
bool operator==(const stackType<Type>& otherStack) const
{
if (this == &otherStack)
{
return true;
}
else
{
if (stackTop != otherStack.stackTop)
{
return false;
}
else
{
for (int i = 0; i < stackTop; i++)
{
if (list[i] != otherStack.list[i])
{
return false;
}
return true;
}
}
}
}
void copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
//copy otherStack into this stack.
for (int j = 0; j < stackTop; j++)
{
list[j] = otherStack.list[j];
}
}
};
int main()
{
string s;
char c;
bool found;
int b = 0;
string j = "";
stackType<double> stack;
cout<<"Would you like to convert an integer(i) or a decimal(d)?"<<endl;
cin>>c;
switch (c) {
case 'i' :
case 'I' : {
cout << "Please enter your integer in string format: ";
cin >> s;
b = atoi(s.c_str());
break;
}
case 'd' :
case 'D' : {
cout << "Please enter your decimal in string format: ";
cin >> s;
found = false;
int q = 0;
while(found == false) {
if(s[q] == '.') {
found = true;
}
else {
q++;
}
}
for (int i = 0; i <q; i++) {
char p = s[i];
j += p;
for (int m = 0; m<q-i; m++) {
j += '0';
}
double k = stof(j);
stack.push(k);
j.clear();
}
break;
}
default: {
cout <<"Wrong input. Please enter i or d for integer or decimal: ";
cin>>c;
break;
}
}
cout << "Here is your string in integer or decimal format: ";
double t = 0;
if(c == 'i') {
cout << b;
}
else if(c == 'd') {
for(int i = 0; i < stack.top(); i++){
t += stack.list[i];
}
cout << t;
}
return 0;
}
I expect the output to be the number printed out correctly as to when I entered it but the output is :
Would you like to convert an integer(i) or a decimal(d)?
d
Please enter your decimal in string format: 1025.56
stacktop: 1
sh: pause: command not found
stacktop: 2
sh: pause: command not found
stacktop: 3
sh: pause: command not found
stacktop: 4
sh: pause: command not found
Here is your string in integer or decimal format: 9.74742e+232Program ended with exit code: 0
Let the input string be 2345.6789. Since it is a string, its individual characters are stored in the consecutive locations in memory. Now, let's access them sequentially, and execute the following algorithm.
First, evaluate the integer part
int Acc = 0;
Loop 1: Repeat until Input == Decimal point
Input = '2'; Acc = 10 * Acc + (Input - '0') = 10 x 0 + 2 = 2
Input = '3'; Acc = 10 * Acc + (Input - '0') = 10 x 2 + 3 = 23
Input = '4'; Acc = 10 * Acc + (Input - '0') = 10 x 23 + 4 = 234
Input = '5'; Acc = 10 * Acc + (Input - '0') = 10 x 234 + 5 = 2345
Input = '.' (Decimal point); exit Loop1
Next, fill the stack with digits in the fractional part.
auto stack = std::stack<int>
Loop2: Repeat until Input == End of string
Input = '6'; stack.push (Input - '0');
Input = '7'; stack.push (Input - '0');
Input = '8'; stack.push (Input - '0');
Input = '9'; stack.push (Input - '0');
Input = End of string; exit Loop2
Next, pop digits from the stack, and evaluate the fractional part.
double Acc2 = 0;
Loop 3: repeat until stack.empty()
Acc2 = (Acc2 + stack.top()) / 10 = (0 + 9) / 10 = 0.9; stack.pop();
Acc2 = (Acc2 + stack.top()) / 10 = (0.9 + 8) / 10 = 0.89; stack.pop();
Acc2 = (Acc2 + stack.top()) / 10 = (0.89 + 7) / 10 = 0.789; stack.pop();
Acc2 = (Acc2 + stack.top()) / 10 = (0.789 + 6) / 10 = 0.6789; stack.pop();
Finally, add the integer part to the fractional part.
Result = Acc + Acc2
This is a purely academic problem. Convey my regards to your teacher.

need help on debugging my c++ programming (text ascii manupulation)

I recently made a C++ program which encrypts texts based-on the vigenere cipher technique.
The encryption part works fine I think, but the decryption function doesn't seem to output the correct answer in some cases. I was hoping if anyone could have a look at code and tell me what's wrong with it.
CMD dialog:
Enter the key for encrytion:
magic
Enter message No. 1:
I love C programming
Message encrypted:
u rwxq i rdomzcymovi
Decrypted:
i love c pXogramming
Somehow it outputted "X" instead of "r" in this case.............
Here are the codes:
Secret.h:
#ifndef CPP_TUTORIALS_SECRET_H
#define CPP_TUTORIALS_SECRET_H
#include <iostream>
using namespace std;
class Secret {
private:
string message;
string key;
bool toEncrypt;
bool toDecrypt;
public:
bool isToDecrypt() const {
return toDecrypt;
}
Secret(const string &message = "", const string &key = "", bool toEncrypt = false);
~Secret();
void setToDecrypt(bool toDecrypt);
void encrypt();
void decrypt();
void display();
};
#endif //CPP_TUTORIALS_SECRET_H
Secret.cpp
#include "Secret.h"
Secret::Secret(const string &message, const string &key, bool toEncrypt) {
Secret::message = message;
Secret::key = key;
Secret::toEncrypt = toEncrypt;
}
Secret::~Secret(){
}
void Secret::setToDecrypt(bool toDecrypt) {
Secret::toDecrypt = toDecrypt;
}
void Secret::display() {
cout << message << endl;
}
void Secret::encrypt() {
if(toEncrypt) {
int keyAscii[key.length()];
int count = 0;
for(unsigned int i = 0; i < key.length(); i++) {
keyAscii[i] = key.at(i);
}
for(unsigned int i = 0; i < message.length(); i++) {
if (message.at(i) > 64 && message.at(i) < 91) {
message.at(i) = (char)((message.at(i) - 65 + (keyAscii[count] - 97)) % 26 + 97);
}
else if (message.at(i) > 96 && message.at(i) < 123) {
message.at(i) = (char)((message.at(i) - 97 + (keyAscii[count] - 97)) % 26 + 97);
}
else{
message.at(i) = message.at(i);
}
count++;
if(count == key.length()) {
count = 0;
}
}
}
}
void Secret::decrypt() {
if(toDecrypt) {
int keyAscii[key.length()];
int count = 0;
for(unsigned int i = 0; i < key.length(); i++) {
keyAscii[i] = key.at(i);
}
for(unsigned int i = 0; i < message.length(); i++) {
if (message.at(i) > 96 && message.at(i) < 123) {
message.at(i) = (char)((message.at(i) - 97) % 26 - (keyAscii[count] - 97) + 97);
}
else {
message.at(i) = message.at(i);
}
count++;
if (count == key.length()) {
count = 0;
}
}
}
}
main.cpp
#include <limits>
#include "Secret.h"
void calcMsgAmount(int &pArraySize);
void inputKey(string &key);
void encryptMsg(Secret &secret, string &msg, const string &key, bool toEncrypt, int index);
int main() {
int arraySize;
string key;
string msg;
calcMsgAmount(arraySize);
inputKey(key);
Secret secrets[arraySize];
for(int i = 0; i < arraySize; i++){
encryptMsg(secrets[i], msg, key, true, i);
}
cout << endl << "Message encrypted: " << endl;
for(Secret i: secrets){
i.display();
i.setToDecrypt(true);
if(i.isToDecrypt()){
i.decrypt();
cout << endl << "Decrypted: " << endl;
i.display();
}
cout << endl << endl;
}
return 0;
}
void calcMsgAmount(int &pArraySize) {
cout << "Enter the amount of messages you want to input: " << endl;
while(!(cin >> pArraySize)){
cout << endl << "There's something really wrong with your input, please enter again." << endl;
cout << "Enter the amount of messages you want to input: " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
void inputKey(string &key){
cout << "Enter the key for encrytion: " << endl;
cin.ignore();
getline(cin, key);
}
void encryptMsg(Secret &secret, string &msg, const string &key, bool toEncrypt, int index){
cout << "Enter message No. " << index + 1 << ": " << endl;
getline(cin, msg);
secret = Secret(msg, key, toEncrypt);
secret.encrypt();
}
Many thanks
accroding to wikipedia the decrypt routine of Vigenere Cipher is
(C - K) mod 26
what you wrote in Secret::decrypt() is
message.at(i) = (char)((message.at(i) - 97) % 26 - (keyAscii[count] - 97) + 97);
which is C % 26 - K.
I changed the line to
message.at(i) = (char)((26 + (message.at(i) - 97) - (keyAscii[count] - 97)) % 26 + 97);
it seems to be correct. I haven't really understood why the first 26 is necessary, but without it the code doesn't work (something with % and negative numbers)
P.S. as for debugging part, you might have noticed that whong letters appear as wrong capital letters which have smaller ascii code, so you decrypt routine has negative numbers in it. After which you check your code against Wikipedia :)
The problem comes in the decrypt from two problems:
the first is that the calculation should be symetric from the the encryp, i.e. the modulo 26 for message - key (as already pointed out by effenok)
the second is that in the decrypt you can at char level, message-key can be negative, which might not give you the expected results modulo 26 (for example -2 % 26 is -2 so that you'll be out of the alphabetic range that you expect). The easy trick is to add 26 before doing the modulo, which makes sure it's done on a positive number.
Here slightly more compact functions, making the difference between uppercase and lowercase :
void Secret::encrypt() {
if(toEncrypt) {
for(unsigned int i = 0; i < message.length(); i++) {
if(isalpha(message[i])){
char start = isupper(message[i]) ? 'A' : 'a';
message[i] = (message[i] - start + (tolower(key[i%key.length()]) - 'a') + 26) % 26 + start;
}
}
}
}
void Secret::decrypt() {
if(toDecrypt) {
for(unsigned int i = 0; i < message.length(); i++) {
if (isalpha(message[i]) ){
char start = isupper(message[i]) ? 'A':'a';
message[i] = (message[i] - start - (tolower(key[i%key.length()]) - 'a') + 26) % 26 + start;
}
}
}
}

sortId: identifier not found

I'm trying to sort the students by first name, grade, and id.
When I run it, it gives me the error "identifier not found."
and I'm kind of confused because I have the void sortsID in the struct before the main.
This is in my main which includes my myDate.h. I only included some of the code that I thought was important in this question
main.cpp
struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};
int main() {
studentData myClass[10];
myClass[0].name = "blah blah";
//adding names to class
//random id numbers, bday, grade for each student
for (int i = 0; i < 10; i++) {
int randomId = (rand() % (9999 - 1000 + 1)) + 1000;
int randomGrade = (rand() % (100 - 50 + 1)) + 50;
int randomMonth = rand() % (12 - 1) + 1;
int randomDay = rand() % (31 - 1) + 1;
int randomYear = rand() % (1994 - 1990) + 1990;
myDate randomBday(randomMonth, randomDay, randomYear);
myClass[i].id = randomId;
myClass[i].grade = randomGrade;
myClass[i].birthday = randomBday;
}
studentData *idSort[10];
case 2:
cout << "Sorting by ID" << endl;
sortID(myClass, 10);
cout << "Displaying original list";
cout << "Student Info:\n";
cout << "=================================================\n";
cout << "NAME ID# GRADE BDAY\n";
for (int i = 0; i < 10; i++) {
idSort[i] = &(myClass[i]);
cout << left << setw(18) << idSort[i]->name << " ";
cout << left << setw(12) << idSort[i]->id << " ";
cout << setw(11) << setprecision(4) << idSort[i]->grade << " ";
idSort[i]->birthday.display();
cout << " " << endl;
}
break;
system("PAUSE"); };
void sortID(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].student_number is greater than s[i+1].student_number,
swap the records
if (s[i].id > s[i + 1].id)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
void sortGrade(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].student_number is greater than s[i+1].student_number, swap the records
if (s[i].grade > s[i + 1].grade)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
void sort_on_name(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].name is later in alphabet than s[i+1].name, swap the two records
if (strcmp(s[i].name, s[i + 1].name) > 0)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
`
You declared function sortID as a non-static member function of class studentData
struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};
But you call it as a non-member function
sortID(myClass, 10);
Of course the compiler does not know how this name is declared because you declared name
studentData::sortID
Moreover the function defined after main with name sortID differs from the member function because its first parameter has type studentData s[] while the early declared function has the first parameter of type studentData
void sortID(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].student_number is greater than s[i+1].student_number,
swap the records
if (s[i].id > s[i + 1].id)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
Thank you guys for all your help!
I just used the #include < algorithmn >
So I just added this between the studentData and main()
struct compare_student_by_id {
bool operator() (const studentData & lhs, const studentData & rhs) {
return lhs.id < rhs.id;
}};
and included
std::sort(myClass, myClass + 10, compare_student_by_id());
in the main.

Palindrome Function

I need to rewrite the program to use a function isPalindrome. It needs to input a 5 digit integer and return a boolean (true if it is a palindrome, false if it is not), and it cannot contain any cout statements. I am not sure how I would do this without a cout function. Here is my code:
#include <iostream>
using namespace std;
int main()
{
int number, digit1, digit2, digit3, digit4, digit5;
cout << "\nEnter a 5-digit integer: ";
cin >> number;
//Break down input number into individual digits:
digit1 = number / 10000;
digit2 = number % 10000 / 1000;
digit3 = number % 10000 / 100;
digit4 = number % 10000 / 10;
digit5 = number % 10;
if ( digit1 == digit5 && digit2 == digit4 )
cout << number <<" is a palindrome.\n";
else
cout << number << " is not a palindrome.\n";
return 0;
}
int isPalindrome ()
{
}
This should help get you started (without ruining too much of the fun)
int main(){
//accept integer input using cin
if(isPalindrome(input))
cout << "yaay!";
else
cout << "nooouuu!";
}
bool isPalindrome (int input)
{
//test things here
return ( digit1 == digit5 && digit2 == digit4 )
// ^ returns true if condition satisfied
}
Additionally, your way of separating out the digits is incorrect. It should be:
digit1 = number/10000 % 10;
digit2 = number/1000 % 10;
digit3 = number/100 % 10;
digit4 = number/10 % 10;
digit5 = number % 10;
Ofcourse, the above should actually be in a loop.
It doesn't have to be specified how many digits does the number contain. You can try something like this:
bool isPalindrome(int number) {
int reverse = 0, copy = number;
while(copy != 0) {
reverse = reverse*10 + copy%10;
copy /= 10;
}
return number == reverse;
}
string s;
cout<<"\nEnter a string : " ;
cin>>s;
int length = s.length();
char* arr = new char();
int k = length;
for(int i = 0 ; i <= length ; i++)
{
arr[i] = s[k];
k -= 1;
}
if(!palindrome(s, arr, length))
{
cout<<"\nNot Palindrome\n";
}
else
{
cout<<"\nPalindrome\n";
}
}
bool palindrome(string& s, char* arr, int length)
{
int j = 0;
for(int i = 1 ; i <= length; i++)
{
if(arr[i]!= s[j])
{
return false;
}
j++;
}
return true;
}