Stuck while trying to create loop while checking for palindrome.

I am writing a program that will check whether or not specific text input is a palindrome or not.
I'm a little stuck trying to fit in my loop while checking the text.
Any help would be greatly appreciated.


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

using namespace std;

int main()
{
char strn[80];
cout<<"Please enter your text input: ";
cin.getline(strn,80);
int len=strlen(strn);
int loop=0;


bool palin=true; 

for(int c=0;c!=len/2;c++) 
{
if(palin) 
{
if(strn[c]!=strn[len-c-1]) 
{
palin=false; 
}

}
else
{
break; 
}
}



if(palin)
{
cout<<"Your text input is a palindrome.";
}
else
{
cout<<"Your text input is not Palindrome.";
}

cin.get();
return 0;

	
	}



Last edited on
It might be simpler to use std::string and std::reverse. You could copy the string, reverse it and check for equality. It might be less efficient but would be much simpler. there is a getline that reads into a std::string.

I'm not sure what your specific problem is but I don't have time to debug it now. It seems like a fairly simply problem to debug if you just step line by line and analyze the state of the variables. Try that if you would like to make it work with the for loop. I wonder what happens to your program if there are an odd number of characters.
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
#include "stdafx.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()

{
    string palindrome;
    
    cout << "Please enter a palindrome:";
    
    getline(cin,palindrome);
    
    
    cout << "You entered: " << palindrome << endl;
    // copy the string
    string copyOfInput(palindrome);
    std::reverse(copyOfInput.begin(), copyOfInput.end());
    if(palindrome == copyOfInput)
    {
        cout << "Word is palindrome" << endl; 
    }
    else
    {
        cout << "Word isn't a palindrome" << endl; 
    }
    cin.get();//to make a pause
    
    return (0);
        
}


Not sure what happened to other thread, but here is a simple example.
I'm sorry, I haven't really messed with header files much.

What exactly would I need to put in the header file, and the source file that goes with it?
Topic archived. No new replies allowed.