1

I'm getting this error hence I am new to c++ I could not understand please help me!!!

I am writing a palindrome code This is the code given below:.............

I am basically here using some extra concepts not doing in-direct fashion. if anyone can post the correct code he/she is most welcome...

//palindrome
#include <cstring>  //or use #include <string.h>
#include <iostream>
using namespace std; 

void Palindrom(string& );

void Palindrome(string& word)// same as (const string& word)
{ 
    int n = word.length();
    string word2;
    char reverse[n]; 

 for(int i = 0; i <=n; i++){
        word[i]=tolower(word[i]);
    }
    word2=word; //now both are small

    for(int i = n-1; i >=0; i--){
        reverse[n-1-i]=word2[i];
        cout<<reverse[n-1-i];
    } 


    for(int i =0; i >n; i++){    //printing reversed 
        cout<< " Reverse:  "<<reverse[i]<<endl;
    } 


    // word is ok and word2 gets reversed  

    for (int i = 0; i <= n; i++){ 
       if(word[i]==reverse[i])
       {
           cout<<"\nit is palandrome ";

       }
    cout<<"\nit is not a palindrome ";
    }
}

int main()
{  string k="YuUuy";
   void Palindrome(k);
   return 0;
}
Dinokor
  • 73
  • 7
  • Possible duplicate of [Check string for palindrome](https://stackoverflow.com/questions/4138827/check-string-for-palindrome) – nima moradi Jul 07 '19 at 17:27

2 Answers2

4

Correct syntax for calling a function is Palindrome(k); without the void.

Few remarks:

  • Get a good c++ book.
  • // same as (const string& word) is not true.

  • You did not include <string> header.

  • It's good practice to use std::size_t for indices, but beware of unsigned>=0 condition being always true.
  • char reverse[n]; is wrong, n must be a compile-time constant, VLA are not part of the C++ standard.
Quimby
  • 17,735
  • 4
  • 35
  • 55
2

Function calls should not have return type. Change void Palindrome(k); in main() function to Palindrome(k);

While declaring array, the expression should have constant value. So you can't use char reverse[n];. Change it to char *reverse = new char[n]; and deallocate it using delete[] reverse; after you are done using it.

I would recommend you to use smart pointer. You should also take a look at std::string instead of using stream of char.

SolidMercury
  • 973
  • 6
  • 15