Get the last position of a string

In my isPalindrome function, I keep getting an error when I try a get the last position to find if a string is a palindrome or not.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

bool isPalindrome(const char*);

int main()
{
   string input;
   fstream nameFile;
   
   
   nameFile.open("Xample.txt", ios::in);
   
   
   if (nameFile)
   {
       cout << "Now reading from file: " << endl;
       // Read an item from the file.
       getline(nameFile, input);
       
       // While the last read operation 
       // was successful, continue.
       while (nameFile)
       {
          
	  cout << input << endl;
	  //Palindrome function call
	  if(isPalindrome(input.c_str())){
		cout << "It is a Palindrome :) " << endl; 
          }
	  else
	 	cout << "It is not a Palindrome :'( " << endl;
          // Read the next string.
          getline(nameFile, input); 
       }
       
       //Close when completed
       cout << "Done!" << endl;
       nameFile.close();
   }
   else
   {
      cout << "ERROR: Cannot open file.\n";
   }
   return 0;
}


//Function to test if string is a palindrome
bool isPalindrome(const char *input){


int first = 0;
int len = strlen(input); //here is my problem
int last = len - 1;


//begin loop to compare first position with last
while(last > first){
	//Loop to 
	while(!isalnum(input[first]) && !isalnum(input[last])){
		first++;
		last--;
		}
	if(tolower(input[first]) != tolower(input[last])){
		return false;
		}
	
	last--;
	first++;
	
	}

	
	return true;





}
figured it out. Forgot to #include <cstring> -_-
Topic archived. No new replies allowed.