1

Check if a string is palindrome I was using the link above to try to solve this problem (among many others, Ive been trying to solve it various ways all day with no dice, this is my first C++). All other examples are usually in an array format, and I can't make assumptions as to the length of a word. I'm trying to make a program to detect if a word is a palindrome or not. I have a text file with one word per line, and want to test each word, line by line, if it is a palindrome, and if so to print it to the screen, and if not, to ignore it and not print it. I figured the best way to locate the palindromes was to reverse the word line by line and match it to the original, and if they are the same (==), then to print it. Here is what I have so far:

#include <iostream> 
#include <vector> 
#include <string> 
#include <cctype>
#include <fstream>

using namespace std; 

int main(int argc, char *argv[]) {

    std::string line;
    std::ifstream infile("wordlist.txt");

}

string reverse(string line){
    if (line == string(line.rbegin(), line.rend())) {
        cout << string;
    }
}

All help is appreciated

Community
  • 1
  • 1
  • "Here is what I have so far".... that's not much, is it? – DevSolar Feb 29 '16 at 04:28
  • Have you tried to call the function? – Andrey Lyubimov Feb 29 '16 at 04:32
  • 1
    I am not surprised that your program is not producing the correct answer. You have not written any code to actually read the words from the list or do anything with them. All this program does is tries to open a file, and then exits. You also have forgotten to return a value from either of your functions. – paddy Feb 29 '16 at 04:34

2 Answers2

0

I guess your question is a homework question and you would like to get some information on how to complete the C++ coding.

You look not to know how to read file contents in C++. Here's a link of how to do it:

Read file-contents into a string in C++

I am not very sure about what you specifically would like to be answered. If your question is a homework question, here's some info of how to ask:

https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions

Community
  • 1
  • 1
0
#include<iostream>
#include<algorithm>
#include<string.h> 
#include<fstream>

using namespace std; 

int main() {
    string line="", line_rev="";
    ifstream infile;

    infile.open("wordlist.txt");

    do{
       infile>>line;
       line_rev=line;
       reverse(line_rev.begin(), line_rev.end());

      if(line==line_rev)
            cout<<line<<endl;

      }while(getline(infile, line));

//if(infile.is_open()){cout<<"open"<<endl;}     //to check if file is open or not
//else{cout<<"unable to open"<<endl;}
return 0;
}

This is the solution. i dont know why you are writing "string reverse(string line)" out side the main() function.

Dementor
  • 21
  • 1
  • 6
  • Thank you, I had tried something similar to that, but i hadn't used a do while, and yours is far simpler than mine was. I actually didn't see there were replies to this post, and I had made some progress on my own after taking a breather from the frustration. Ghosts from previous attempts were still in my original post, so apologies for the confusion. I will post my solution (not as simple by any stretch) soon once I iron out the kinks. – KittenKibbles Mar 01 '16 at 02:17
  • it doesn’t really matter if your code is complex or simple while you are learning. you can always improvise your code later. – Dementor Mar 01 '16 at 04:36