Converting string to a char array?

How do I convert a string of unknown length to a char array? I am reading strings from a file and checking if the string is a Palindrome or not. I think my palindrome function is correct, but do I do something like char [] array = string.length(); ??

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

using namespace std;

int main()
{
   string input;
   char [] array = input
   fstream nameFile;

   
   
   nameFile.open("Xample.txt", ios::in);
   
   
   if (nameFile)
   {
       // Read an item from the file.
       getline(nameFile, input);
       
       // While the last read operation 
       // was successful, continue.
       while (nameFile)
       {
          
	  isPalindrome(nameFile, input);
          
          // Read the next item.
          getline(nameFile, input); 
       }
       
       
       nameFile.close();
   }
   else
   {
      cout << "ERROR: Cannot open file.\n";
   }
   return 0;
}



bool isPalindrome(fstream nameFile, char [] input){


int first = 0;
int last = input.length() - 1;


while(last > first){
	if(input[first] != input[last]){
		return false;
		cout << "String is not a palindrome." << endl;
	}
last--;
first++;
}

	cout << "String is a palindrome." << endl;
	return true;





}
Last edited on
Topic archived. No new replies allowed.